Java定时器Timer简述

 更新时间:2017年01月12日 15:04:28   作者:VectorYao   我要评论

本文主要介绍了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:当早于基准时间时,时间差内的执行任务未补偿完时,下一次执行任务的时间参考的是上一次任务的结束时间;一旦补偿完毕,下一次执行任务的时间参考上一次任务的开始时间来计算。 二者一样。下一次任务的执行时间都是参考上一次任务的结束时间来计算。
晚于当前基准时间 二者一样。下一次任务的执行时间参考的是上一次任务的开始时间来计算。 二者一样。下一次任务的执行时间都是参考上一次任务的结束时间来计算。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

相关文章

  • java冒泡排序和选择排序示例

    java冒泡排序和选择排序示例

    这篇文章主要介绍了java冒泡排序和选择排序示例,需要的朋友可以参考下
    2014-05-05
  • Java实现判断浏览器版本与类型简单代码示例

    Java实现判断浏览器版本与类型简单代码示例

    这篇文章主要介绍了Java实现判断浏览器版本与类型简单代码示例,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • java简单网页抓取的实现方法

    java简单网页抓取的实现方法

    这篇文章主要介绍了java简单网页抓取的实现方法,详细分析了与Java网页抓取相关的tcp及URL相关概念,以及对应的类文件原理,具有一定的参考借鉴价值,需要的朋友可以参考下
    2014-12-12
  • java 中HashMap实现原理深入理解

    java 中HashMap实现原理深入理解

    这篇文章主要介绍了java 中HashMap实现原理深入理解的相关资料,需要的朋友可以参考下
    2017-03-03
  • Java中生成随机数的实现方法总结

    Java中生成随机数的实现方法总结

    这篇文章主要介绍了Java中生成随机数的实现方法总结,其中多线程并发的实现方式尤为exciting,需要的朋友可以参考下
    2015-11-11
  • 23种设计模式(18)java备忘录模式

    23种设计模式(18)java备忘录模式

    这篇文章主要为大家详细介绍了23种设计模式之java备忘录模式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • 浅谈springBoot注解大全

    浅谈springBoot注解大全

    本篇文章主要介绍了浅谈springBoot注解大全,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • Java三种循环求和方法

    Java三种循环求和方法

    本篇文章给大家介绍了Java三种循环求和的方法,大家在学程序的时候如果能用的到,参考下吧。
    2018-02-02
  • Java多线程模式之Balking模式详解

    Java多线程模式之Balking模式详解

    这篇文章主要介绍了Java多线程模式之Balking模式,结合实例形式较为详细的分析了Balking模式的原理、用法与相关注意事项,需要的朋友可以参考下
    2017-06-06
  • Java内部类原理与用法实例总结

    Java内部类原理与用法实例总结

    这篇文章主要介绍了Java内部类原理与用法,结合实例形式总结分析了非静态内部类、静态内部类、局部类等相关概念、原理、用法及相关操作注意事项,需要的朋友可以参考下
    2018-08-08

最新评论