java定时器 schedule和scheduleAtFixedRate区别(转)
来源:互联网 发布:lv男士名片夹 淘宝 编辑:程序博客网 时间:2024/06/03 14:38
schedule和scheduleAtFixedRate的区别在于,如果指定开始执行的时间在当前系统运行时间之前,scheduleAtFixedRate会把已经过去的时间也作为周期执行,而schedule不会把过去的时间算上。
比如
SimpleDateFormat fTime = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”);
Date d1 = fTime.parse(“2005/12/30 14:10:00”);
t.scheduleAtFixedRate(new TimerTask(){
public void run()
{
System.out.println(“this is task you do6”);
}
},d1,3*60*1000);
间隔时间是3分钟,指定开始时间是2005/12/30 14:10:00,如果我在14:17:00分执行这个程序,那么会立刻打印3次
this is task you do6 //14:10
this is task you do6 //14:13
this is task you do6 //14:16
并且注意,下一次执行是在14:19 而不是 14:20。就是说是从指定的开始时间开始计时,而不是从执行时间开始计时。
但是上面如果用schedule方法,间隔时间是3分钟,指定开始时间是2005/12/30 14:10:00,那么在14:17:00分执行这个程序,则立即执行程序一次。并且下一次的执行时间是 14:20,而不是从14:10开始算的周期(14:19)。
需要注意的是scheduleAtFixedRate和schedule在参数完全相同的情况下,执行效果是不同的。上面例子中任务只是打印一个字符串,比较简单。但如果任务比较复杂,或者由于任何原因(如垃圾回收或其他后台活动)而延迟了某次执行,则scheduleAtFixedRate方法将快速连续地出现两次或更多的执行,从而使后续执行能够“追赶上来”;而schedule方法的后续执行也将被延迟。所以,在长期运行中,scheduleAtFixedRate执行的频率将正好是指定周期的倒数,schedule 执行的频率一般要稍慢于指定周期的倒数。
scheduleAtFixedRate 效率总体上高于schedule
- java定时器 schedule和scheduleAtFixedRate区别(转)
- java定时器 schedule和scheduleAtFixedRate区别
- schedule和scheduleAtFixedRate区别
- schedule和scheduleAtFixedRate区别
- schedule和scheduleAtFixedRate区别
- schedule和scheduleAtFixedRate区别
- schedule和scheduleAtFixedRate区别
- schedule() 和 scheduleAtFixedRate() 区别
- java中的定时器以及schedule和scheduleAtFixedRate方法的探究
- (转)schedule和scheduleAtFixedRate
- schedule和scheduleAtFixedRate的区别
- Time schedule和scheduleAtFixedRate区别
- Time schedule和scheduleAtFixedRate区别
- [java]Timer的schedule和scheduleAtFixedRate方法的区别解析
- java Timer 的 schedule 和 scheduleAtFixedRate 方法的区别
- TImer定时器的方法schedule和scheduleAtFixedRate区别已经动态修改定时计划的实现
- Timer的schedule和scheduleAtFixedRate的区别
- Timer的schedule和scheduleAtFixedRate的区别
- eclipse中.java文件和.html文件怎么不同编码方式
- leetcode--Factorial Trailing Zeroes
- 通过浏览器渲染过程来进行前端优化
- 动态代理模式示例-接上文
- Makefile学习笔记(二)
- java定时器 schedule和scheduleAtFixedRate区别(转)
- php 获取变量的整数值 intval()
- java使用validator进行校验
- 二叉搜索树与双向链表
- Matlab 的基本结构
- filebeat-5.1.1配置详解
- AndroidStudio坑爹错误之一
- 关于pokeman go
- linux内核源码树建立