在java中,Timer类主要用于定时性、周期性任务 的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下
(1)schedule方法:“fixed-delay”;如果第一次执行时间被delay了,随后的执行时间按 照 上一次 实际执行完成的时间点 进行计算
(2)scheduleAtFixedRate方法:“fixed-rate”;如果第一次执行时间被delay了,随后的执行时间按照 上一次开始的 时间点 进行计算,并且为了”catch up”会多次执行任务,TimerTask中的执行体需要考虑同步
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date startDate = dateFormatter.parse("2010/11/26 00:20:00");
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){
public void run()
{
System.out.println("execute task!" + this.scheduledExecutionTime());
}
},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秒钟
package test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Test {
public static void main(String[] args) throws ParseException {
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date startDate = dateFormatter.parse("2010/11/28 01:06:00");
Timer timer = new Timer();
timer.schedule(new TimerTask(){
public void run() {
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("execute task!"+ this.scheduledExecutionTime());
}
},startDate, 5 * 1000);
}
}
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秒,而当前任务已经开始执行了,因此两个任务间存在重叠,需要考虑线程同步
http://blog.csdn.net/gtuu0123/article/details/6040159 原址
分享到:
相关推荐
主要为大家详细介绍了java中timer的schedule和scheduleAtFixedRate方法区别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
NULL 博文链接:https://michael1990.iteye.com/blog/2113141
java关于Timer schedule执行定时任务
今天小编就为大家分享一篇关于Java中Timer的schedule()方法参数详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
c# ScheduleTimer_src 自定义 c# ScheduleTimer_src
主要介绍了Python timer定时器两种常用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1、Timer和TimerTask在调用cancel()取消后不能再执行 schedule语句 2、只能在UI主线程中更新控件/组件。在其他线程中,更新控件/组件 https://blog.csdn.net/qq_31939617/article/details/80118302
当表格已经收集完毕,要收集的日期和时间已经到达, ScheduleTimer控件将通知运用程序。 Purple Parrot ScheduleTimer ActiveX Control 的功能: 如下图所指示在调度程序形式上显示系统预置选择项。 选择...
Timer控件的使用方法介绍
nios II TIMER中断解析及程序
C语言02-Timer0-Timer1-Timer2-Timer3-Timer4测试程序(STC32G-DEMO-CODE-220311kw)C语言02-Timer0-Timer1-Timer2-Timer3-Timer4测试程序(STC32G-DEMO-CODE-220311kw)C语言02-Timer0-Timer1-Timer2-Timer3-Timer4...
使用Timer控件创建一个网页时钟,要求无刷新的方式进行时间的更新和显示
最新单片机仿真 TIMER0与TIMER1控制条形LED最新单片机仿真 TIMER0与TIMER1控制条形LED最新单片机仿真 TIMER0与TIMER1控制条形LED最新单片机仿真 TIMER0与TIMER1控制条形LED最新单片机仿真 TIMER0与TIMER1控制条形LED...
单片机C语言程序设计 TIMER0与TIMER1控制条形LED(有源码)单片机C语言程序设计 TIMER0与TIMER1控制条形LED(有源码)单片机C语言程序设计 TIMER0与TIMER1控制条形LED(有源码)单片机C语言程序设计 TIMER0与TIMER1控制...
使用System.Threading.Timer 使用System.Timers.Timer 分别举例这两个类来实现多线程的时钟,不会阻塞UI线程 例子是Winform的
Java中的Timer和TimerTask简介(附完整代码实例)。 在Java中,Timer和TimerTask是两个经常用来实现定时器的类。这两个类使用起来非常方便,可以完成我们对定时器的绝大多数需求。 Timer是一种定时器工具,用来在一...
本文实例讲述了C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析,分享给大家供大家参考。具体分析如下: 在.NET Framework里面提供了三种Timer ① System.Windows.Forms.Timer ② System.Timers.Timer ③...
本工程为java工程,使用MyEclipse新建。