程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java定時器Timer簡述

Java定時器Timer簡述

編輯:關於JAVA

Java定時器Timer簡述。本站提示廣大學習愛好者:(Java定時器Timer簡述)文章只能為提供參考,不一定能成為您想要的結果。以下是Java定時器Timer簡述正文


概述

次要用於Java線程裡指定時間或周期運轉義務。Timer是線程平安的,但不提供實時性(real-time)保證。

結構函數

Timer()

默許結構函數。

Timer(boolean)

指定關聯線程能否作為daemon線程。

Timer(String)

指定關聯線程的稱號。

Timer(String, boolean)

同時指定關聯線程的稱號和能否作為daemon。

schdule辦法

schedule(TimerTask task, long delay)

以以後時間為基准,延遲指定的毫秒後執行一次TimerTask義務。

schedule(TimerTask task, Date time)

在指定的日期執行一次TimerTask義務。

假如日期time早於以後時間,則立即執行。

運用示例

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();
 }
 }
}

執行後果

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

闡明是立即執行。

schedule(TimerTask task, long delay, long period)

以以後時間為基准,延遲指定的毫秒後,再按指定的時間距離地有限次數的執行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秒有限次數的執行。

schedule(TimerTask task, Date firstTime, long period)

在指定的日期之後,按指定的時間距離地有限次數的執行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

闡明指定的之間早於以後時間,則立即執行,不會補充時間差內的義務。

scheduleAtFixedRate辦法

scheduleAtFixedRate(TimerTask task, long delay, long period)

以以後時間為基准,延遲指定的毫秒後,再按指定的時間距離周期性地有限次數的執行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秒有限次數的執行。

scheduleAtFixedRate(TimerTask task, Date firstTime, long period)

在指定的日期之後,按指定的時間距離周期性地有限次數的執行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秒執行一次定時義務。

執行義務延時比照之 schedule 和 scheduleAtFixedRate

schedule不延時

運用示例

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 2016

Process 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 2016

Process finished with exit code 0

不論早還是晚於基准時間,都不停止補償,下一次義務的執行時間參考的是上一次義務的開端時間點來計算。

schedule延時

運用示例

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 2016

Process 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 2016

Process finished with exit code 0

不論早還是晚於以後基准時間,都不停止補償,下一次義務的執行時間都是參考上一次義務完畢的時間點來計算。

scheduleAtFixedRate不延時

運用示例

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 2016

Process 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 2016

Process finished with exit code 0

不延時的狀況下,當早於基准時間時,時間差內的執行義務未補償完時,下一次執行義務的時間參考的是上一次執行義務的完畢時間;一旦補償終了(留意粗體時間點),下一次執行義務的時間參考的是上一次執行義務的開端時間;當晚於基准時間時,下一次執行義務的時間參考的是上一次執行義務的開端時間。

scheduleAtFixedRate延時

運用示例

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 2016

Process finished with exit code 0

延時的狀況下,即便是早於基准時間,由於延時效應,基本不能夠補償終了時間差內的執行義務,故而在延時的狀況下,下一次義務的執行時間都是參考上一次義務完畢的時間來計算。

比照總結

執行義務不延時 執行義務延時 早於以後基准時間 schedule:下一次義務的執行時間參考的是上一次義務的開端時間來計算。 scheduleAtFixedRate:當早於基准時間時,時間差內的執行義務未補償完時,下一次執行義務的時間參考的是上一次義務的完畢時間;一旦補償終了,下一次執行義務的時間參考上一次義務的開端時間來計算。 二者一樣。下一次義務的執行時間都是參考上一次義務的完畢時間來計算。 晚於以後基准時間 二者一樣。下一次義務的執行時間參考的是上一次義務的開端時間來計算。 二者一樣。下一次義務的執行時間都是參考上一次義務的完畢時間來計算。

以上就是本文的全部內容,希望本文的內容對大家的學習或許任務能帶來一定的協助,同時也希望多多支持!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved