在java中,Timer类主要用于定时性、周期性任务 的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下

(1)schedule方法:“fixed-delay”;如果第一次执行时间被delay了,随后的执行时间  上一次 实际执行完成的时间点 进行计算
(2)scheduleAtFixedRate方法:“fixed-rate”;如果第一次执行时间被delay了,随后的执行时间按照 上一次开始的 时间点 进行计算,并且为了”catch up”会多次执行任务,TimerTask中的执行体需要考虑同步

  1. SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  2. Date startDate = dateFormatter.parse("2010/11/26 00:20:00");
  3. Timer timer = new Timer();
  4. timer.scheduleAtFixedRate(new TimerTask(){
  5. public void run()
  6. {
  7. System.out.println("execute task!" + this.scheduledExecutionTime());
  8. }
  9. },startDate,3*60*1000);

以上的代码,表示在2010-11-26 00:20:00秒开始执行,每3分钟执行一次
假设在2010/11/26 00:27:00执行
以上会打印出3次
execute task!   00:20
execute task!   00:23    catch up
execute task!   00:26    catch up
下一次执行时间是00:29,相对于00:26
当换成schedule方法时,在2010/11/26 00:27:00执行
会打印出1次
execute task!   00:20   无catch up
下一次执行时间为00:30,相对于00:27

以上考虑的都是在你设定的timer开始时间后,程序才被执行

当执行任务的时间大于周期间隔时,会发生什么呢?
(1)schedule方法:下一次执行时间相对于 上一次 实际执行完成的时间点 ,因此执行时间会不断延后
(2)scheduleAtFixedRate方法:下一次执行时间相对于上一次开始的 时间点 ,因此执行时间不会延后,存在并发性 
以下例程序来测试上述结论,TimerTask需要执行6秒钟,但是间隔周期为5秒钟

  1. package test;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.Timer;
  6. import java.util.TimerTask;
  7. public class Test {
  8. public static void main(String[] args) throws ParseException {
  9. SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  10. Date startDate = dateFormatter.parse("2010/11/28 01:06:00");
  11. Timer timer = new Timer();
  12. timer.schedule(new TimerTask(){
  13. public void run() {
  14. try {
  15. Thread.sleep(6000);
  16. } catch (InterruptedException e) {
  17. e.printStackTrace();
  18. }
  19. System.out.println("execute task!"+ this.scheduledExecutionTime());
  20. }
  21. },startDate, 5 * 1000);
  22. }
  23. }

schedule方法的执行结果如下:
execute task!1290877560001
execute task!1290877566001
execute task!1290877572001
execute task!1290877578001
execute task!1290877584001
execute task!1290877590001
execute task!1290877596001
execute task!1290877602001
execute task!1290877608001
execute task!1290877614001
execute task!1290877620001
execute task!1290877626001
execute task!1290877632001
execute task!1290877638001
可以看出,间隔时间都为6秒,因此,下一次的执行时间点=上一次程序执行完成的时间点+间隔时间 
当换成scheduleAtFixedRate方法的执行结果如下:
execute task!1290877860000
execute task!1290877865000
execute task!1290877870000
execute task!1290877875000
execute task!1290877880000
execute task!1290877885000
execute task!1290877890000
execute task!1290877895000
execute task!1290877900000
execute task!1290877905000
execute task!1290877910000
execute task!1290877915000
execute task!1290877920000
execute task!1290877925000
execute task!1290877930000
可以看出,间隔时间都为5秒,因此,下一次的执行时间点=上一次程序开始执行的时间点+间隔时间 ;并且因为前一个任务要执行6秒,而当前任务已经开始执行了,因此两个任务间存在重叠,需要考虑线程同步

Timer的schedule和scheduleAtFixedRate方法的区别解析(转)的更多相关文章

  1. Timer的schedule和scheduleAtFixedRate方法的区别解析

    在java中,Timer类主要用于定时性.周期性任务 的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下 (1)sched ...

  2. 简单理解java中timer的schedule和scheduleAtFixedRate方法的区别

    timer的schedule和scheduleAtFixedRate方法一般情况下是没什么区别的,只在某个情况出现时会有区别--当前任务没有来得及完成下次任务又交到手上. 我们来举个例子: 暑假到了老 ...

  3. Java中timer的schedule()和schedualAtFixedRate()函数的区别

    本文主要讨论java.util.Timer的schedule(timerTask,delay,period)和scheduleAtFixedRate(timerTask,delay,period)的区 ...

  4. schedule和scheduleAtFixedRate区别

    需求: 由于系统长期运作,各设备之间产生很多信息,一段时间后需要清除数据 考虑方案: 用schedule还是scheduleAtFixedRate,在此比较分析了下这两个的区别 schedule和sc ...

  5. 定时任务调度工作(学习记录 四)schedule与scheduleAtFixedRate的区别

    根据两种情况来看区别 一.首次计划执行的时间早于当前的时间 1.schedule方法 “fixed-delay”:如果第一次执行时间被延迟了,随后的执行时间按照上一次实际执行完成的时间点进行计算 演示 ...

  6. schedule() 和 scheduleAtFixedRate() 的区别--转载

    1.  schedule() ,2个参数方法:在执行任务时,如果指定的计划执行时间scheduledExecutionTime <= systemCurrentTime,则task会被立即执行. ...

  7. Timer类的schedule和scheduleAtFixedRate 简单应用

    Timer类可以用作定时任务,主要的方法有schedule和scheduleAtFixedRate. schedule(TimerTask task, Date time) 安排在指定的时间执行指定的 ...

  8. schedule与scheduleAtFixedRate之Timer源码分析

    执行Timer任务调度方法有如下几种: 这些方法最后调用的都是这个方法: private void sched(TimerTask task, long time, long period)   这个 ...

  9. ScheduledExecutorService中scheduleAtFixedRate方法与scheduleWithFixedDelay方法的区别

    ScheduledExecutorService中scheduleAtFixedRate方法与scheduleWithFixedDelay方法的区别 ScheduledThreadPoolExecut ...

随机推荐

  1. Linux下anaconda的安装

    http://peteryuan.net/use-anaconda/ http://www.jianshu.com/p/03d757283339

  2. WPF之X名称空间学习

    WPF的X名称空间都有什么呢?首先,盗用张图来说明: 我将就图表中的内容进行总结: 1.x:Array具有一个Iteams属性,它能暴漏一个ArratList实例,ArratList实例的内部成员类型 ...

  3. UOJ #55 & 洛谷 P3920 紫荆花之恋 —— 动态点分治+替罪羊树

    题目:http://uoj.ac/problem/55 https://www.luogu.org/problemnew/show/P3920 参考博客:https://www.cnblogs.com ...

  4. jenkins 参数化构建和增加环境变量

    1.参数化构建 2.增加环境变量 prepare an environment for the run,需要安装Environment Injector插件

  5. IP Fragmentation(IP分片)

    https://www.cisco.com/c/en/us/tech/ip/index.html IP协议在传输数据包时,将数据报文分为若干分片进行传输,并在目标系统中进行重组,这一过程称为分片(Fr ...

  6. [python] itertools库学习

    最近做 cyber-dojo上的题,好几道都要用到排列组合.一开始我还老老实实自己写算法.后来一想,不对呀!python有那么多的库,为啥不用呢? 于是搜了下,发现这个:itertools 使用 he ...

  7. postgresql 主从复制并切换

    1 环境 192.168.19.145 Kylin 3.3 mysqlhq  9.5.2  psql_master192.168.19.227 Kylin 3.3 mysql3    9.5.2  p ...

  8. Linux打包下载命令

    语法:tar [主选项+辅选项] 文件或者目录使用该命令时,主选项是必须要有的,它告诉tar要做什么事情,辅选项是辅助使用的,可以选用. 主选项: c 创建新的档案文件.如果用户想备份一个目录或是一些 ...

  9. 2016.4.6 WinForm显示PDF两种方法

    1.最直接的方法,添加webbrowser控件 webb.Url = new Uri(path);可显示pdf控件. 如果需要在打开时跳转到某页,可用在路径后直接加#page=,例如webb.Url ...

  10. qt安装必要的库 qt开源安装包下载

    yum install mesa-libGL-devel mesa-libGLU-devel #yum install freeglut-devel http://www.qt.io/download ...