主要用於Java線程裏指定時間或週期運行任務。Timer
是線程安全的,但不提供實時性(real-time)保證。java
默認構造函數。安全
指定關聯線程是否做爲daemon線程。ide
指定關聯線程的名稱。函數
同時指定關聯線程的名稱和是否做爲daemon。線程
以當前時間爲基準,延遲指定的毫秒後執行一次TimerTask任務。code
在指定的日期執行一次TimerTask任務。orm
若是日期time早於當前時間,則馬上執行。it
使用示例io
public class Demo { private static Timer timer = new Timer(); public static class MyTask extends TimerTask { @Override public void run() { System.out.println("Run Time:" + new Date().toString()); } } public static void main(String[] args) { try { MyTask task = new MyTask(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = "2016-12-27 14:36:00"; Date date = sdf.parse(dateStr); System.out.println("Date = " + date.toString() + " NowTime = " + new Date().toString()); timer.schedule(task, date); } catch (ParseException e) { e.printStackTrace(); } } }
執行結果table
Date = Tue Dec 27 14:36:00 CST 2016 NowTime = Tue Dec 27 21:28:04 CST 2016
Run Time:Tue Dec 27 21:28:04 CST 2016
說明是馬上執行。
以當前時間爲基準,延遲指定的毫秒後,再按指定的時間間隔地無限次數的執行TimerTask任務。(fixed-delay execution)
使用示例
public class Demo { private static Timer timer = new Timer(); public static class MyTask extends TimerTask { @Override public void run() { System.out.println("Run Time: " + new Date().toString()); } } public static void main(String[] args) { MyTask task = new MyTask(); System.out.println("Now Time: " + new Date().toString()); timer.schedule(task, 3000, 5000); } }
執行結果
Now Time: Tue Dec 27 21:34:59 CST 2016
Run Time: Tue Dec 27 21:35:02 CST 2016
Run Time: Tue Dec 27 21:35:07 CST 2016
Run Time: Tue Dec 27 21:35:12 CST 2016
Run Time: Tue Dec 27 21:35:17 CST 2016
說明以當前基準時間延遲3秒後執行一次,之後按指定間隔時間5秒無限次數的執行。
在指定的日期以後,按指定的時間間隔地無限次數的執行TimerTask任務。(fixed-delay execution)
若是日期firstTime早於當前時間,則馬上執行,且不執行在時間差內的任務。
使用示例
public class Demo { private static Timer timer = new Timer(); public static class MyTask extends TimerTask { @Override public void run() { System.out.println("Run Time:" + new Date().toString()); } public static void main(String[] args) { try { MyTask task = new MyTask(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = "2016-12-27 14:36:00"; Date date = sdf.parse(dateStr); System.out.println("Date = " + date.toString() + " NowTime = " + new Date().toString()); timer.schedule(task, date, 3000); } catch (ParseException e) { e.printStackTrace(); } } } }
執行結果
Date = Tue Dec 27 14:36:00 CST 2016 NowTime = Tue Dec 27 21:43:30 CST 2016
Run Time:Tue Dec 27 21:43:30 CST 2016
Run Time:Tue Dec 27 21:43:33 CST 2016
Run Time:Tue Dec 27 21:43:36 CST 2016
說明指定的之間早於當前時間,則馬上執行,不會補充時間差內的任務。
以當前時間爲基準,延遲指定的毫秒後,再按指定的時間間隔週期性地無限次數的執行TimerTask任務。(fixed-rate execution)
使用示例
public class Demo { private static Timer timer = new Timer(); public static class MyTask extends TimerTask { @Override public void run() { System.out.println("Run Time: " + new Date().toString()); } } public static void main(String[] args) { MyTask task = new MyTask(); System.out.println("Now Time: " + new Date().toString()); timer.scheduleAtFixedRate(task, 3000, 5000); } }
執行結果
Now Time: Tue Dec 27 21:58:03 CST 2016
Run Time: Tue Dec 27 21:58:06 CST 2016
Run Time: Tue Dec 27 21:58:11 CST 2016
Run Time: Tue Dec 27 21:58:16 CST 2016
Run Time: Tue Dec 27 21:58:21 CST 2016
說明以當前基準時間延遲3秒後執行一次,之後按指定間隔時間5秒無限次數的執行。
在指定的日期以後,按指定的時間間隔週期性地無限次數的執行TimerTask任務。(fixed-rate execution)
若是日期firstTime早於當前時間,則當即執行,並補充性的執行在時間差內的任務。
使用示例
public class Demo { private static Timer timer = new Timer(); public static class MyTask extends TimerTask { @Override public void run() { System.out.println("Run Time:" + new Date().toString()); } public static void main(String[] args) { try { MyTask task = new MyTask(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = "2016-12-27 22:02:00"; Date date = sdf.parse(dateStr); System.out.println("Date = " + date.toString() + " NowTime = " + new Date().toString()); timer.scheduleAtFixedRate(task, date, 5000); } catch (ParseException e) { e.printStackTrace(); } } } }
執行結果
Date = Tue Dec 27 22:02:00 CST 2016 NowTime = Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:55 CST 2016
Run Time:Tue Dec 27 22:03:00 CST 2016
Run Time:Tue Dec 27 22:03:05 CST 2016
說明指定的之間早於當前時間,則馬上執行。
在時間22:02:00--22:02:54內大約有11個5秒間隔,則優先補充性的執行在時間差內的任務,而後在22:02:55補充完畢(執行12次。ps:0-55秒區間段內首位都算上,正好觸發12次),此後每隔5秒執行一次定時任務。
使用示例
public class Demo { private static Timer timer = new Timer(); private static int runCount = 0; public static class MyTask extends TimerTask { @Override public void run() { try { System.out.println("Begin Run Time: " + new Date().toString()); Thread.sleep(3000); System.out.println("End Run Time: " + new Date().toString()); runCount++; if (runCount == 3) { timer.cancel(); } } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { try { MyTask task = new MyTask(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = "2016-12-27 14:36:00"; Date date = sdf.parse(dateStr); System.out.println("Date = " + date.toString() + " NowTime = " + new Date().toString()); timer.schedule(task, date, 5000); } catch (ParseException e) { e.printStackTrace(); } } }
執行結果
早於當前基準時間
Date = Tue Dec 27 14:36:00 CST 2016 NowTime = Tue Dec 27 22:23:37 CST 2016
Begin Run Time: Tue Dec 27 22:23:37 CST 2016
End Run Time: Tue Dec 27 22:23:40 CST 2016
Begin Run Time: Tue Dec 27 22:23:42 CST 2016
End Run Time: Tue Dec 27 22:23:45 CST 2016
Begin Run Time: Tue Dec 27 22:23:47 CST 2016
End Run Time: Tue Dec 27 22:23:50 CST 2016Process finished with exit code 0
晚於當前基準時間
Date = Tue Dec 27 22:42:00 CST 2016 NowTime = Tue Dec 27 22:41:54 CST 2016
Begin Run Time: Tue Dec 27 22:42:00 CST 2016
End Run Time: Tue Dec 27 22:42:03 CST 2016
Begin Run Time: Tue Dec 27 22:42:05 CST 2016
End Run Time: Tue Dec 27 22:42:08 CST 2016
Begin Run Time: Tue Dec 27 22:42:10 CST 2016
End Run Time: Tue Dec 27 22:42:13 CST 2016Process finished with exit code 0
無論早仍是晚於基準時間,都不進行補償,下一次任務的執行時間參考的是上一次任務的開始時間點來計算。
使用示例
public class Demo { private static Timer timer = new Timer(); private static int runCount = 0; public static class MyTask extends TimerTask { @Override public void run() { try { System.out.println("Begin Run Time: " + new Date().toString()); Thread.sleep(5000); System.out.println("End Run Time: " + new Date().toString()); runCount++; if (runCount == 3) { timer.cancel(); } } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { try { MyTask task = new MyTask(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = "2016-12-27 22:42:00"; Date date = sdf.parse(dateStr); System.out.println("Date = " + date.toString() + " NowTime = " + new Date().toString()); timer.schedule(task, date, 3000); } catch (ParseException e) { e.printStackTrace(); } } }
執行結果
早於當前基準時間
Date = Tue Dec 27 22:42:00 CST 2016 NowTime = Tue Dec 27 22:45:17 CST 2016
Begin Run Time: Tue Dec 27 22:45:17 CST 2016
End Run Time: Tue Dec 27 22:45:22 CST 2016
Begin Run Time: Tue Dec 27 22:45:22 CST 2016
End Run Time: Tue Dec 27 22:45:27 CST 2016
Begin Run Time: Tue Dec 27 22:45:27 CST 2016
End Run Time: Tue Dec 27 22:45:32 CST 2016Process finished with exit code 0
晚於當前基準時間
Date = Tue Dec 27 22:47:00 CST 2016 NowTime = Tue Dec 27 22:46:27 CST 2016
Begin Run Time: Tue Dec 27 22:47:00 CST 2016
End Run Time: Tue Dec 27 22:47:05 CST 2016
Begin Run Time: Tue Dec 27 22:47:05 CST 2016
End Run Time: Tue Dec 27 22:47:10 CST 2016
Begin Run Time: Tue Dec 27 22:47:10 CST 2016
End Run Time: Tue Dec 27 22:47:15 CST 2016Process finished with exit code 0
無論早仍是晚於當前基準時間,都不進行補償,下一次任務的執行時間都是參考上一次任務結束的時間點來計算。
使用示例
public class Demo { private static Timer timer = new Timer(); private static int runCount = 0; public static class MyTask extends TimerTask { @Override public void run() { try { System.out.println("Begin Run Time: " + new Date().toString()); Thread.sleep(3000); System.out.println("End Run Time: " + new Date().toString()); runCount++; if (runCount == 1000) { timer.cancel(); } } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { try { MyTask task = new MyTask(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = "2016-12-27 22:51:42"; Date date = sdf.parse(dateStr); System.out.println("Date = " + date.toString() + " NowTime = " + new Date().toString()); timer.scheduleAtFixedRate(task, date, 5000); } catch (ParseException e) { e.printStackTrace(); } } }
執行結果
早於當前基準時間
Date = Tue Dec 27 22:51:42 CST 2016 NowTime = Tue Dec 27 22:51:57 CST 2016
Begin Run Time: Tue Dec 27 22:51:57 CST 2016
End Run Time: Tue Dec 27 22:52:00 CST 2016
Begin Run Time: Tue Dec 27 22:52:00 CST 2016
End Run Time: Tue Dec 27 22:52:03 CST 2016
Begin Run Time: Tue Dec 27 22:52:03 CST 2016
End Run Time: Tue Dec 27 22:52:06 CST 2016
Begin Run Time: Tue Dec 27 22:52:06 CST 2016
End Run Time: Tue Dec 27 22:52:09 CST 2016
Begin Run Time: Tue Dec 27 22:52:09 CST 2016
End Run Time: Tue Dec 27 22:52:12 CST 2016
Begin Run Time: Tue Dec 27 22:52:12 CST 2016
End Run Time: Tue Dec 27 22:52:15 CST 2016
Begin Run Time: Tue Dec 27 22:52:15 CST 2016
End Run Time: Tue Dec 27 22:52:18 CST 2016
Begin Run Time: Tue Dec 27 22:52:18 CST 2016
End Run Time: Tue Dec 27 22:52:21 CST 2016
Begin Run Time: Tue Dec 27 22:52:22 CST 2016
End Run Time: Tue Dec 27 22:52:25 CST 2016
Begin Run Time: Tue Dec 27 22:52:27 CST 2016
End Run Time: Tue Dec 27 22:52:30 CST 2016
Begin Run Time: Tue Dec 27 22:52:32 CST 2016
End Run Time: Tue Dec 27 22:52:35 CST 2016
Begin Run Time: Tue Dec 27 22:52:37 CST 2016
End Run Time: Tue Dec 27 22:52:40 CST 2016
Begin Run Time: Tue Dec 27 22:52:42 CST 2016
End Run Time: Tue Dec 27 22:52:45 CST 2016
Begin Run Time: Tue Dec 27 22:52:47 CST 2016
End Run Time: Tue Dec 27 22:52:50 CST 2016
Begin Run Time: Tue Dec 27 22:52:52 CST 2016
End Run Time: Tue Dec 27 22:52:55 CST 2016
Begin Run Time: Tue Dec 27 22:52:57 CST 2016
End Run Time: Tue Dec 27 22:53:00 CST 2016Process finished with exit code 0
晚於當前基準時間
Date = Tue Dec 27 22:37:00 CST 2016 NowTime = Tue Dec 27 22:36:06 CST 2016
Begin Run Time: Tue Dec 27 22:37:00 CST 2016
End Run Time: Tue Dec 27 22:37:03 CST 2016
Begin Run Time: Tue Dec 27 22:37:05 CST 2016
End Run Time: Tue Dec 27 22:37:08 CST 2016
Begin Run Time: Tue Dec 27 22:37:10 CST 2016
End Run Time: Tue Dec 27 22:37:13 CST 2016Process finished with exit code 0
不延時的狀況下,當早於基準時間時,時間差內的執行任務未補償完時,下一次執行任務的時間參考的是上一次執行任務的結束時間;一旦補償完畢(注意粗體時間點),下一次執行任務的時間參考的是上一次執行任務的開始時間;當晚於基準時間時,下一次執行任務的時間參考的是上一次執行任務的開始時間。
使用示例
public class Demo { private static Timer timer = new Timer(); private static int runCount = 0; public static class MyTask extends TimerTask { @Override public void run() { try { System.out.println("Begin Run Time: " + new Date().toString()); Thread.sleep(5000); System.out.println("End Run Time: " + new Date().toString()); runCount++; if (runCount == 3) { timer.cancel(); } } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { try { MyTask task = new MyTask(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = "2016-12-27 22:28:00"; Date date = sdf.parse(dateStr); System.out.println("Date = " + date.toString() + " NowTime = " + new Date().toString()); timer.scheduleAtFixedRate(task, date, 3000); } catch (ParseException e) { e.printStackTrace(); } } }
執行結果
早於當前基準時間
Date = Tue Dec 27 23:01:00 CST 2016 NowTime = Tue Dec 27 23:01:19 CST 2016
Begin Run Time: Tue Dec 27 23:01:19 CST 2016
End Run Time: Tue Dec 27 23:01:24 CST 2016
Begin Run Time: Tue Dec 27 23:01:24 CST 2016
End Run Time: Tue Dec 27 23:01:29 CST 2016
Begin Run Time: Tue Dec 27 23:01:29 CST 2016
End Run Time: Tue Dec 27 23:01:34 CST 2016
Begin Run Time: Tue Dec 27 23:01:34 CST 2016
End Run Time: Tue Dec 27 23:01:39 CST 2016
晚於當前基準時間
Date = Tue Dec 27 22:28:00 CST 2016 NowTime = Tue Dec 27 22:27:55 CST 2016
Begin Run Time: Tue Dec 27 22:28:00 CST 2016
End Run Time: Tue Dec 27 22:28:05 CST 2016
Begin Run Time: Tue Dec 27 22:28:05 CST 2016
End Run Time: Tue Dec 27 22:28:10 CST 2016
Begin Run Time: Tue Dec 27 22:28:10 CST 2016
End Run Time: Tue Dec 27 22:28:15 CST 2016Process finished with exit code 0
延時的狀況下,即便是早於基準時間,因爲延時效應,根本不可能補償完畢時間差內的執行任務,故而在延時的狀況下,下一次任務的執行時間都是參考上一次任務結束的時間來計算。
執行任務不延時 | 執行任務延時 | |
---|---|---|
早於當前基準時間 | schedule:下一次任務的執行時間參考的是上一次任務的開始時間來計算。 scheduleAtFixedRate:當早於基準時間時,時間差內的執行任務未補償完時,下一次執行任務的時間參考的是上一次任務的結束時間;一旦補償完畢,下一次執行任務的時間參考上一次任務的開始時間來計算。 | 兩者同樣。下一次任務的執行時間都是參考上一次任務的結束時間來計算。 |
晚於當前基準時間 | 兩者同樣。下一次任務的執行時間參考的是上一次任務的開始時間來計算。 | 兩者同樣。下一次任務的執行時間都是參考上一次任務的結束時間來計算。 |
注意:scheduleAtFixedRate示例中firstTime是有區別的,而結果證實是有補償性操做的。
(END)