在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命令学习(21):netstat命令

    版权声明 更新:2017-06-13博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的netstat ...

  2. Why getting this error “django.db.utils.OperationalError: (1050, ”Table 'someTable' already exists“)”

    0down votefavorite   I am getting error like django.db.utils.OperationalError: (1050, "Table 's ...

  3. Oracle中查询前10条记录

    在Oracle怎样查询表中的top10条记录呢? select * from test where rownum <=10     ----说明:rownum只能用于<或<=运算,如 ...

  4. mysql之 [ERROR] InnoDB: Unable to lock ./ibdata1, error: 11

    问题描述:启动MySQL后,出现连接不上,报 [ERROR] InnoDB: Unable to lock ./ibdata1, error: 11[root@mysql01 ~]# service ...

  5. php 数组NULL元素的批量处理

    $a = array('a'=>1, 'b'=>0, 'c'=>NULL); foreach($a as $k=>$v) {  // if( is_null($a[$k]))  ...

  6. java中io的详解

    注:本文全篇转载于:http://blog.csdn.net/taxueyingmei/article/details/7697042,觉得讲的挺详细,就借过来看看,挺不错的文章. 先贴一张图 Jav ...

  7. hl7 v2.X 版本中RSP_K23消息的构造

    RSP_K23消息有MSH, MSA, ERR, QAK, QPD, PID几个segment,其中ERR,PID为可选. 1. 当MSA有err时,ERR段填充出错的详细信息. 2. 当MSA为AA ...

  8. What makes an inferred latch? how To avoid creating inferred latches? when do you know you need latches?

    What makes an inferred latch?For combinatorial logic, the output of the circuit is a function of inp ...

  9. Hot resize Multipath Disk – Linux

    This post is for the users of the great dm-multipath system in Linux, who encounter a major availabi ...

  10. Linux cciss磁盘设备文件的说明

    在某些机器上安装Linux后,发现在/dev目录下找不到hda.hdb.sda等磁盘设备文件,那么挂接的磁盘 在哪里呢?使用mount命令查看挂接设备情况,发现磁盘文件在.dev\cciss目录下,并 ...