在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. Scala 面向接口编程

    1.如果要实现一个接口,前边没有extends关键字就可以使用extends,如果有要使用with关键字 2.Scala 中的接口支持多种继承,类或者抽象类不支持多种继承 3.抽象属性:未被实例化的属 ...

  2. 《Javascript高级程序设计》阅读记录(四):第五章 下

    这个系列,我会把阅读<Javascript高级程序设计>之后,感觉讲的比较深入,而且实际使用价值较大的内容记录下来,并且注释上我的一些想法.做这个一方面是提升了我的阅读效果以及方便我以后阅 ...

  3. [HihoCoder1413]Rikka with String

    vjudge 题意 给你一个串,问你把每个位置的字符替换成#后串中有多少本质不同的子串. \(n\le 3*10^5\) sol 首先可以计算出原串里面有多少本质不同的子串.显然就是\(\sum_{i ...

  4. vmem驱动设备

    vmem是内存多字符设备.包含vfs的open.read.write.ioctl.poll.fasync和release函数,device文件的读写. virtual_mem.c #include & ...

  5. Linux开放80、8080端口或者开放某个端口

    装载系统的时候只开启了22端口.结果再装完Nginx+php+mysql 后不能访问网站. 查看防火墙设置发现没开启80端口 iptables -L -n 由于Linux防火墙默认是关闭的.可以用两种 ...

  6. 增加 [确定] and [失败]系统提示

    增加 [确定] and  [失败]系统提示 #!/bin/bash. /etc/init.d/functionsaction "true" /bin/falseaction &qu ...

  7. FPGA的年龄

    FPGA的年龄 1984年,Xilinx公司发布了第一个FPGA(但直到1985年这些器件才真正发货).尽管这些器件比当时那些简单的可编程逻辑器件(PLD)复杂的多,但大多数数字设计工程师却仅仅用这些 ...

  8. Day2-VIM(六): 恢复

    恢复在VIM里比较简单,不过想要具体恢复到某个时间段很难 就我的经验而言,有时候使用恢复还不如删了重写 这里我们来讲讲恢复.撤销和重复命令的使用 u 撤消上次命令 U 恢复整行 ctrl+r 重做 . ...

  9. 开发工具:sublime text3安装Vue语法高亮插件

    一.将插件克隆到Sublime的packages目录 1.下载并解压插件(或直接git命令clone),得到vue-syntax-highlight-master文件夹 插件地址:https://gi ...

  10. zookeeper 安装配置注意事项

    zoo.cfg 1.server.1/2/3  有几台配置几个 ​2.配置好hosts映射之后可以用node1替代IP地址 3.dataLogDir  下面配置的logs 的目录一定要创建 4.dat ...