筆記來源: IMOOC Java Timer
schedule(task, time)
html
參數java
做用android
schedule(task, time, period)
併發
參數app
做用ide
schedule(task, delay)
函數
參數工具
做用this
schedule(task, delay, period)
spa
參數
做用
scheduleAtFixedRate(task, time, period)
參數
做用
scheduleAtFixedRate(task, delay, period)
參數
做用
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.TimerTask; public class MyTimerTask extends TimerTask { private String name; public MyTimerTask(String inputName) { name = inputName; } @Override public void run() { // 以 yyyy-MM-dd HH:mm:ss 的格式打印當前執行時間 Calendar calendar = Calendar.getInstance(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Current exec time is: " + sf.format(calendar.getTime())); // 打印當前name的內容 System.out.println("Current exec name is: " + name); } public String getName() { return name; } public void setName(String name) { this.name = name; } }
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Timer; public class MyTimer { public static void main(String[] args) { // 1. 建立一個timer實例 Timer timer = new Timer(); // 2. 建立一個MyTimerTask實例 MyTimerTask myTimerTask = new MyTimerTask("No.1"); /** * 獲取當前時間,並設置成距離當前時間三秒後的時間 */ Calendar calendar = Calendar.getInstance(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Current main time is: " + sf.format(calendar.getTime())); calendar.add(Calendar.SECOND, 3); // 3. 經過timer定時定頻率調用myTimerTask的業務邏輯 //-------------------schedule的四種用法-------------------// /** * 在時間等於或超過time的時間執行且僅執行一次task */ myTimerTask.setName("schedule_1"); timer.schedule(myTimerTask, calendar.getTime()); /** * 時間等於或超過time時首次執行task,以後每隔period毫秒重複執行一次task */ myTimerTask.setName("schedule_2"); timer.schedule(myTimerTask, calendar.getTime(), 2000); /** * 等待delay毫秒後執行且僅執行一次task */ myTimerTask.setName("schedule_3"); timer.schedule(myTimerTask, 1000); /** * 等待delay毫秒後首次執行task,以後每隔period毫秒重複執行一次task */ myTimerTask.setName("schedule_4"); timer.schedule(myTimerTask, 3000, 2000); //--------------scheduleAtFixedRate的兩種用法-------------// /** * 時間等於或超過time時首次執行task,以後每隔period毫秒重複執行一次task */ myTimerTask.setName("scheduleAtFixedRate_1"); timer.scheduleAtFixedRate(myTimerTask, calendar.getTime(), 2000); /** * 等待delay毫秒後首次執行task,以後每隔period毫秒重複執行一次task */ myTimerTask.setName("scheduleAtFixedRate_2"); timer.scheduleAtFixedRate(myTimerTask, 3000, 2000); } }
schedule
方法
fixed-delay
;若是第一次執行時間被 delay 了,隨後的執行時間按照上一次實際執行完成的時間點進行計算scheduleAtFixedRate
方法
fixed-rate
;若是第一次執行時間被 delay 了,隨後的執行時間按照上一次開始的時間點進行計算,而且爲了遇上進度會屢次執行任務,所以 TimerTask
中的執行體須要考慮同步 schedule
方法
scheduleAtFixedRate
方法
TimerTask
的 cancel()
、scheduleExecutionTime()
Timer
的 cancel()
、purge()
cancel()
TimerTask
裏的任務scheduleExecutionTime()
cancel()
purge()
模擬兩個機器人的定時行爲
/** * 跳舞的機器人 */ public class DancingRobot extends TimerTask { @Override public void run() { // 獲取最近的一次任務執行的時間,並將其格式化 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Scheduled exec time is: " + sf.format(scheduledExecutionTime())); System.out.println("Dancing happily!"); } } /** * 倒水的機器人 */ public class WaterRobot extends TimerTask { private Timer timer; // 最大容量爲5L private Integer bucketCapacity = 0; public WaterRobot(Timer inputTimer) { timer = inputTimer; } @Override public void run() { // 灌水直接至桶滿爲止 if(bucketCapacity < 5){ System.out.println("Add 1L water into the bucket!"); bucketCapacity ++; } else { // 水滿以後就中止執行 System.out.println("The number of canceled task in timer is: " + timer.purge()); cancel(); System.out.println("The waterRobot has been aborted"); System.out.println("The number of canceled task in timer is: " + timer.purge()); System.out.println("Current water is " + bucketCapacity + "L"); // 等待兩秒鐘,終止timer裏面的全部內容 try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } timer.cancel(); } } } public class Executor { public static void main(String[] args) { Timer timer = new Timer(); // 獲取當前時間 Calendar calendar = Calendar.getInstance(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Current time is: " + sf.format(calendar.getTime())); DancingRobot dr = new DancingRobot(); WaterRobot wr = new WaterRobot(timer); timer.schedule(dr, calendar.getTime(), 2000); timer.scheduleAtFixedRate(wr, calendar.getTime(), 1000); } }
管理併發任務的缺陷
當任務拋出異常時的缺陷
TimerTask
拋出 RuntimeException
,Timer 會中止全部任務的運行