บทความนี้ส่วนใหญ่แบ่งปันรหัสตัวอย่างเกี่ยวกับการกำหนดเวลางาน ScheduleDexecutorService ดังต่อไปนี้:
รหัสตัวอย่าง
แพ็คเกจ com.effective.common.concurrent.execute; นำเข้า java.text.dateformat; นำเข้า java.text.parseException; นำเข้า java.text.simpledateFormat; นำเข้า java.util.date; นำเข้า java.util.concurrent.executors; java.util.concurrent.timeUnit; กำหนดการคลาสสาธารณะ {ส่วนตัวคงที่ dateformat dateformat = ใหม่ simpledateFormat ("yy-mm-dd hh: mm: ss"); วันที่แบบคงที่ส่วนตัว dayformat = simpledateformat ใหม่ ("yy-mm-dd"); Executors.newsinglethreathedscheduledExecutor ();/** * ดำเนินงานตามระยะเวลาความถี่ที่ระบุ <br> * การเริ่มต้นการเริ่มต้นเริ่มต้นการดำเนินการโดย 0ms และงานจะถูก executes ทุก ๆ 5ms */โมฆะสาธารณะคงที่ () {excutor.scheduleatfixedrate (echoserver ใหม่ (), // การดำเนินการเธรด 0, // การเริ่มต้นล่าช้า 5000, // ช่วงเวลาขั้นต่ำระหว่างการประหารชีวิตสองครั้ง echoserver (), // การดำเนินการเธรด 0, // การเริ่มต้นล่าช้า 5000, //timeunit.milliseconds); }/** * ดำเนินการทุกวันเวลา 20.00 น. */โมฆะสาธารณะ dayofdelay (เวลาสตริง) getTimemillis ("20:00:00") - System.currentTimeMillis (); InitDelay = InitDelay> 0? InitDelay: OneDay + InitDelay; Executor.ScheduleatFixedrate (ใหม่ echoserver (), initDelay, oneday, timeUnit.milliseconds);}/** * รับจำนวนมิลลิวินาที {วันที่ currentDate = dateFormat.parse (dayformat.format (วันที่ใหม่ ()) + "" + เวลา); ส่งคืน currentDate.getTime ();} catch (parseexception e) {e.printstacktrace ();} return 0; กำหนดการ FIXEDRATE (); scement.fixdelay ();}}สรุป
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้เกี่ยวกับตัวอย่างรหัสเวลางาน Tame ScheduleDexecutorService ฉันหวังว่ามันจะเป็นประโยชน์กับทุกคน เพื่อนที่สนใจสามารถอ้างถึงหัวข้ออื่น ๆ ที่เกี่ยวข้องในเว็บไซต์นี้ต่อไป หากมีข้อบกพร่องใด ๆ โปรดฝากข้อความไว้เพื่อชี้ให้เห็น ขอบคุณเพื่อนที่ให้การสนับสนุนเว็บไซต์นี้!