schedule和scheduleAtFixedRate区别
需求:
由于系统长期运作,各设备之间产生很多信息,一段时间后需要清除数据
考虑方案:
用schedule还是scheduleAtFixedRate,在此比较分析了下这两个的区别
schedule和scheduleAtFixedRate的区别在于,如果指定开始执行的时间在当前系统运行时间之前,scheduleAtFixedRate会把已经过去的时间也作为周期执行,而schedule不会把过去的时间算上。
schedule和scheduleAtFixedRate 区别:
(1) 2个参数的schedule在制定任务计划时, 如果指定的计划执行时间scheduledExecutionTime<= systemCurrentTime,则task会被立即执行。scheduledExecutionTime不会因为某一个task的过度执行而改变。
(2) 3个参数的schedule在制定反复执行一个task的计划时,每一次执行这个task的计划执行时间随着前一次的实际执行时间而变,也就是 scheduledExecutionTime(第n+1次)=realExecutionTime(第n次)+periodTime。也就是说如果第n 次执行task时,由于某种原因这次执行时间过长,执行完后的systemCurrentTime>= scheduledExecutionTime(第n+1次),则此时不做时隔等待,立即执行第n+1次task,而接下来的第n+2次task的 scheduledExecutionTime(第n+2次)就随着变成了realExecutionTime(第n+1次)+periodTime。说 白了,这个方法更注重保持间隔时间的稳定。
(3)3个参数的scheduleAtFixedRate在制定反复执行一个task的计划时,每一次 执行这个task的计划执行时间在最初就被定下来了,也就是scheduledExecutionTime(第n次)=firstExecuteTime +n*periodTime;如果第n次执行task时,由于某种原因这次执行时间过长,执行完后的systemCurrentTime>= scheduledExecutionTime(第n+1次),则此时不做period间隔等待,立即执行第n+1次task,而接下来的第n+2次的 task的scheduledExecutionTime(第n+2次)依然还是firstExecuteTime+(n+2)*periodTime这 在第一次执行task就定下来了。说白了,这个方法更注重保持执行频率的稳定。
package TimerMG;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/***
* [Schedule]
* @author Visec丶Dana
* schedule方法:“fixed-delay”;
* 如果第一次执行时间被delay了,随后的执行时间按 照 上一次 实际执行完成的时间点 进行计算
*/
public class ScheduleWay {
public static void main(String[] args) throws ParseException {
final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = dateFormatter.parse("2014-11-14 10:30:00");
Timer timer = new Timer();
timer.schedule(new TimerTask(){
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("execute task! "+ dateFormatter.format(this.scheduledExecutionTime()));
}
},startDate, 5 * 1000);
}
}
execute task! 2014-11-14 11:24:14
execute task! 2014-11-14 11:24:19
execute task! 2014-11-14 11:24:24
execute task! 2014-11-14 11:24:29
ScheduleAtFixed
package TimerMG; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
* [ScheduleAtFixed]
* @author Visec丶Dana
* fixed-rate;如果第一次执行时间被delay了,
* 随后的执行时间按照 上一次开始的 时间点 进行计算,
* 并且为了”catch up”会多次执行任务,TimerTask中的执行体需要考虑同步
*/
public class ScheduleAtFixedRateWay{
public static void main(String[] args) throws ParseException {
final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = dateFormatter.parse("2014-11-14 10:30:00");
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){
public void run()
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("execute task! " + dateFormatter.format(this.scheduledExecutionTime()));
}
},startDate,5*1000);
}
}
execute task! 2014-11-14 10:30:00
execute task! 2014-11-14 10:30:05
execute task! 2014-11-14 10:30:10
execute task! 2014-11-14 10:30:15
execute task! 2014-11-14 10:30:20
schedule和scheduleAtFixedRate区别的更多相关文章
- schedule() 和 scheduleAtFixedRate() 区别
1. schedule() ,2个参数方法:在执行任务时,如果指定的计划执行时间scheduledExecutionTime <= systemCurrentTime,则task会被立即执行. ...
- java定时器schedule和scheduleAtFixedRate区别
package cn.lonecloud.test; import java.util.Date; import java.util.Timer; import java.util.TimerTask ...
- 简单理解java中timer的schedule和scheduleAtFixedRate方法的区别
timer的schedule和scheduleAtFixedRate方法一般情况下是没什么区别的,只在某个情况出现时会有区别--当前任务没有来得及完成下次任务又交到手上. 我们来举个例子: 暑假到了老 ...
- schedule() 和 scheduleAtFixedRate() 的区别--转载
1. schedule() ,2个参数方法:在执行任务时,如果指定的计划执行时间scheduledExecutionTime <= systemCurrentTime,则task会被立即执行. ...
- Timer的schedule和scheduleAtFixedRate方法的区别解析(转)
在java中,Timer类主要用于定时性.周期性任务 的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下 (1)sched ...
- Timer的schedule和scheduleAtFixedRate方法的区别解析
在java中,Timer类主要用于定时性.周期性任务 的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下 (1)sched ...
- Timer类的schedule和scheduleAtFixedRate 简单应用
Timer类可以用作定时任务,主要的方法有schedule和scheduleAtFixedRate. schedule(TimerTask task, Date time) 安排在指定的时间执行指定的 ...
- schedule与scheduleAtFixedRate比较
schedule与scheduleAtFixedRate: 不延时: schedule(TimerTask, Date runDate, long period)方法任务不延时----Date类型 i ...
- 定时任务调度工作(学习记录 四)schedule与scheduleAtFixedRate的区别
根据两种情况来看区别 一.首次计划执行的时间早于当前的时间 1.schedule方法 “fixed-delay”:如果第一次执行时间被延迟了,随后的执行时间按照上一次实际执行完成的时间点进行计算 演示 ...
随机推荐
- 设备版本,设备号,APP版本,APP名称获取
//获取设备id号 UIDevice *device = [UIDevice currentDevice];//创建设备对象 NSString *deviceUID = [[NSString allo ...
- .NET中的访问修饰符
.NET中一共有五种访问修饰符 分别是 public 公共的,访问权限最高的. private ...
- JS匿名函数自执行函数
JS匿名函数自执行函数:(function(){})();(function(){}) 这是一个函数,函数后面接(),则是调用函数 比如(function(arg){console.log(arg); ...
- [译]Memory Reordering Caught in the Act
原文:http://preshing.com/20120515/memory-reordering-caught-in-the-act/ 编写lock-free的C/C++程序时,在保证memory ...
- ASP.net UrlRewrite的防盗链功能
ASP.net中如何实现基于UrlRewrite的防盗链. ASP.net中最快实现UrlRewrite的方法这篇文章中说了如何做UrlRewrite,那只是一个最简单的应用 其实利用UrlRewri ...
- try 返回前执行fianlly
try catch finally 语句中 如果try中有返回语句,如果在fianlly代码块中有对这个值修改的话,并不影响其放回值 public class Test { public stati ...
- 删:Centos 7安装Nginx 1.8
[CentOS 7] 安装nginx! 首先进行 nginx yum Nginx安装记录 注意:如果用源码安装,nginx配置时需要指定--with-pcer对应的压缩包路径,如果使用二进制安装不需要 ...
- C# 委托简单使用方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- STM32F0xx_TIM输入捕获(计算频率)配置详细过程
前言 关于STM32的定时器,可谓是功能强大,估计没有多少人研究完STM32定时器的所有功能(包括我也没有),只是使用常用的一些功能,后续我会推出关于STM32定时器的更多功能. STM32芯片多数为 ...
- 在EF的code frist下写稳健的权限管理系统:仓储设计(三)
public class BaseRepository<T>:IBaseRepository<T> where T : class { protected EfConnecti ...