首先看一下jdk自帶定時器: 一種工具,線程用其安排以後在後臺線程中執行的任務。可安排任務執行一次,或者定期重覆執行。與每個 Timer 對象相對應的是單個後臺線程,用於順序地執行所有計時器任務。計時器任務應該迅速完成。如果完成某個計時器任務的時間太長,那麼它會“獨占”計時器的任務執行線程。因此,這 ...
首先看一下jdk自帶定時器:
一種工具,線程用其安排以後在後臺線程中執行的任務。可安排任務執行一次,或者定期重覆執行。與每個 Timer 對象相對應的是單個後臺線程,用於順序地執行所有計時器任務。計時器任務應該迅速完成。如果完成某個計時器任務的時間太長,那麼它會“獨占”計時器的任務執行線程。因此,這就可能延遲後續任務的執行,而這些任務就可能“堆在一起”,並且在上述不友好的任務最終完成時才能夠被快速連續地執行。
schedule(TimerTask task,long delay) 安排在指定延遲後執行指定的任務。
schedule(TimerTask task,Date time) 安排在指定的時間執行指定的任務。如果此時間已過去,則安排立即執行該任務。
schedule(TimerTask task, long delay, long period) 安排指定的任務從指定的延遲後開始進行重覆的固定延遲執行。如果由於任何原因(如垃圾回收或其他後臺活動)而延遲了某次執行,則後續執行也將被延遲
schedule(TimerTask task,Date firstTime,long period) 安排指定的任務在指定的時間開始進行重覆的固定延遲執行。如果由於任何原因(如垃圾回收或其他後臺活動)而延遲了某次執行,則後續執行也將被延遲。
1 package test; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 import java.util.Timer; 7 import java.util.TimerTask; 8 9 /** 10 * jdk自帶定時器 11 * 12 * @author LIUTIE 13 * 14 */ 15 public class JDKTimer { 16 17 18 public static void main(String[] args) throws ParseException { 19 //日期格式工具 20 final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 21 22 Timer timer = new Timer(); 23 // 10s後執行定時器,僅執行一次 24 System.out.print(sdf.format(new Date())); 25 System.out.println("the timer one will be executed after 10 seconds..."); 26 long milliseconds = 10 * 1000; 27 timer.schedule(new TimerTask() { 28 29 @Override 30 public void run() { 31 System.out.print(sdf.format(new Date())); 32 System.out.println("the timer one has finished execution"); 33 } 34 }, milliseconds); 35 36 //12秒後執行定時器,每1s執行一次 37 System.out.print(sdf.format(new Date())); 38 System.out.println("the timer two will be executed after 12 seconds..."); 39 //啟動後延遲時間 40 long afterSs = 12 * 1000; 41 //執行周期 42 long intervalSs1 = 1 * 1000; 43 timer.schedule(new TimerTask() { 44 // 執行計數器 45 int i = 0; 46 47 @Override 48 public void run() { 49 System.out.print(sdf.format(new Date())); 50 System.out.println("the timer two has execution " + (++i) + " timers"); 51 // 執行10次後關閉定時器 52 if (i == 10) { 53 this.cancel(); 54 } 55 } 56 }, afterSs, intervalSs1); 57 58 59 // 指定時間執行定時器,僅執行一次 60 System.out.print(sdf.format(new Date())); 61 System.out.println("the timer three will be executed at 2017-06-27 21:47:00..."); 62 Date date = sdf.parse("2017-06-27 21:47:00"); 63 timer.schedule(new TimerTask() { 64 65 @Override 66 public void run() { 67 System.out.print(sdf.format(new Date())); 68 System.out.println("the timer three has finished execution"); 69 } 70 }, date); 71 72 // 從指定時間開始周期性執行 73 System.out.print(sdf.format(new Date())); 74 System.out.println("the timer four will be executed at 2017-06-27 21:48:00..."); 75 // 執行間隔周期 76 long intervalSs = 1 * 1000; 77 // 開始執行時間 78 Date beginTime = sdf.parse("2017-06-27 21:48:00"); 79 timer.schedule(new TimerTask() { 80 // 執行計數器 81 int i = 0; 82 83 @Override 84 public void run() { 85 System.out.print(sdf.format(new Date())); 86 System.out.println("the timer four has execution " + (++i) + " timers"); 87 // 執行10次後關閉定時器 88 if (i == 10) { 89 this.cancel(); 90 } 91 } 92 }, beginTime, intervalSs); 93 } 94 95 }View Code
執行結果
2017-06-27 21:46:24the timer one will be executed after 10 seconds... 2017-06-27 21:46:24the timer two will be executed after 12 seconds... 2017-06-27 21:46:24the timer three will be executed at 2017-06-27 21:47:00... 2017-06-27 21:46:24the timer four will be executed at 2017-06-27 21:48:00... 2017-06-27 21:46:34the timer one has finished execution 2017-06-27 21:46:36the timer two has execution 1 timers 2017-06-27 21:46:37the timer two has execution 2 timers 2017-06-27 21:46:38the timer two has execution 3 timers 2017-06-27 21:46:39the timer two has execution 4 timers 2017-06-27 21:46:40the timer two has execution 5 timers 2017-06-27 21:46:41the timer two has execution 6 timers 2017-06-27 21:46:42the timer two has execution 7 timers 2017-06-27 21:46:43the timer two has execution 8 timers 2017-06-27 21:46:44the timer two has execution 9 timers 2017-06-27 21:46:45the timer two has execution 10 timers 2017-06-27 21:47:00the timer three has finished execution 2017-06-27 21:48:00the timer four has execution 1 timers 2017-06-27 21:48:01the timer four has execution 2 timers 2017-06-27 21:48:02the timer four has execution 3 timers 2017-06-27 21:48:03the timer four has execution 4 timers 2017-06-27 21:48:04the timer four has execution 5 timers 2017-06-27 21:48:05the timer four has execution 6 timers 2017-06-27 21:48:06the timer four has execution 7 timers 2017-06-27 21:48:07the timer four has execution 8 timers 2017-06-27 21:48:08the timer four has execution 9 timers 2017-06-27 21:48:09the timer four has execution 10 timersView Code