Brief description of Java timer Timer

  • 2020-05-30 20:02:31
  • OfStack

An overview of the

Mainly used in Java threads to specify time or cycle running tasks. Timer is thread safe, but does not provide a guarantee of real time (real-time).

The constructor

Timer()

Default constructor.

Timer(boolean)

Specifies whether the associated thread is an daemon thread.

Timer(String)

Specifies the name of the associated thread.

Timer(String, boolean)

Also specify the name of the associated thread and whether to act as daemon.

schdule method

schedule(TimerTask task, long delay)

Execute the TimerTask task 1 time after a specified delay of milliseconds based on the current time.

schedule(TimerTask task, Date time)

Perform the TimerTask task once on the specified date.

If the date time is earlier than the current time, it is executed immediately.

Use the sample


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

The execution result


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

The instructions are executed immediately.

schedule(TimerTask task, long delay, long period)

The TimerTask task is executed an unlimited number of times at the specified time interval after a delay of a specified number of milliseconds based on the current time. (fixed - delay execution)

Use the sample


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

The execution result


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

Execute 1 time after the current base time delay of 3 seconds, and then execute an infinite number of times at the specified interval of 5 seconds.

schedule(TimerTask task, Date firstTime, long period)

Perform the TimerTask task an unlimited number of times at specified intervals after the specified date. (fixed - delay execution)

If the date firstTime is earlier than the current time, execute immediately and do not execute the task within the time difference.

Use the sample


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

The execution result


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

If the specified time is earlier than the current time, the task will be executed immediately and the time difference will not be supplemented.

scheduleAtFixedRate method

scheduleAtFixedRate(TimerTask task, long delay, long period)

The TimerTask task is periodically executed an unlimited number of times at the specified time interval after a delay of a specified number of milliseconds based on the current time. (fixed - rate execution)

Use the sample


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

The execution result


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

Execute 1 time after the current base time delay of 3 seconds, and then execute an infinite number of times at the specified interval of 5 seconds.

scheduleAtFixedRate(TimerTask task, Date firstTime, long period)

After the specified date, the TimerTask task is periodically executed an unlimited number of times at the specified time intervals. (fixed - rate execution)

If the date firstTime is earlier than the current time, it is executed immediately, with supplementary execution of the task within the time difference.

Use the sample


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

The execution result


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

If the specified time is earlier than the current time, it is executed immediately.

There are approximately 11 5-second intervals between time 22:02:00 and 22:02:54, and the task within the time difference is prioritized for supplementary execution, and then completed at 22:02:55 (12 executions). ps: the first part of the interval from 0 to 55 seconds is counted, which triggers exactly 12 times), and the timing task is performed every 5 seconds thereafter.

Task execution delay comparison between schedule and scheduleAtFixedRate

schedule don't delay

Use the sample


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
0

The execution result

Earlier than the current base time


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
1

Later than the current base time


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
2

No compensation is made, no matter whether it is earlier or later than the base time. The execution time of the next task is calculated according to the starting time of the previous task.

schedule delay

Use the sample


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

The execution result

Earlier than the current base time


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
4

Later than the current base time


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
5

No compensation will be made whether earlier or later than the current base time, and the execution time of the next task will be calculated according to the time point at which the previous task ends.

scheduleAtFixedRate don't delay

Use the sample


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
6

The execution result

Earlier than the current base time


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
7

Later than the current base time


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

In the case of no delay, when the time is earlier than the benchmark time, the time of the next task is referred to the end time of the previous task when the execution task in the time difference is not fully compensated. Once the compensation is completed (note the bold time point), the time of the next task execution refers to the time of the previous task execution. At the base time of the night, the time of the next task refers to the start time of the previous task.

scheduleAtFixedRate delay

Use the sample


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
9

The execution result

Earlier than the current base time


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

Later than the current base time


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

In the case of delay, even if it is earlier than the benchmark time, it is impossible to compensate for the execution of the task within the time difference due to the delay effect. Therefore, in the case of delay, the execution time of the next task is calculated by referring to the end time of the previous task.

conclusion

执行任务不延时 执行任务延时
早于当前基准时间 schedule:下1次任务的执行时间参考的是上1次任务的开始时间来计算。 scheduleAtFixedRate:当早于基准时间时,时间差内的执行任务未补偿完时,下1次执行任务的时间参考的是上1次任务的结束时间;1旦补偿完毕,下1次执行任务的时间参考上1次任务的开始时间来计算。 2者1样。下1次任务的执行时间都是参考上1次任务的结束时间来计算。
晚于当前基准时间 2者1样。下1次任务的执行时间参考的是上1次任务的开始时间来计算。 2者1样。下1次任务的执行时间都是参考上1次任务的结束时间来计算。


Related articles: