Timer TimerTask schedule scheduleAtFixedRate
jdk 自带的 timer 框架是有缺陷的, 其功能简单,而且有时候它的api 不好理解。
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask; public class TestTimer { private static final int delay = ; /**
* @param args
*/
public static void main(String[] args) {
Timer t = new Timer();
int seconds = ;
TimerTask task = new SimpleTask();
t.schedule(task , delay, seconds * );
} } class SimpleTask extends TimerTask{ @Override
public void run() { System.out.println("SimpleTask.run() 11 " + new Date()); try {
Thread.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
} //System.out.println("SimpleTask.run() 22 " + new Date());
} }
结果为
SimpleTask.run() 11 Mon May 30 16:43:58 CST 2016
SimpleTask.run() 11 Mon May 30 16:44:02 CST 2016
SimpleTask.run() 11 Mon May 30 16:44:06 CST 2016
显示是每隔4秒, 而不是我想象的 period + sleep time 即 2+4 = 6s, why ??
原来是这样的:
参考: http://stackoverflow.com/questions/15128937/java-util-timer-fixed-delay-not-working
Nope, that's how it is intended to work. The period is the period between start times, not the period between an end time and the next start time.
Basically you're telling it in plain english "start at 1 second and execute every two seconds after that" so 1, 3, 5, 7, etc is the logical interpretation.
shareimprove this answer
answered Feb 28 '13 at 6:34
Affe
31k25064
Ok, I re-read the Javadoc again. That means 'schedule' uses previous task's start-time as reference (2nd task will reference 1st task, 3rd task will reference 2nd task). While 'scheduleAtFixedRate' always use the first task's start-time as reference (all task's start-time are based on the 1st task's start-time). Is that how it works? – Dave Xenos Feb 28 '13 at 6:52
Yeah, the way I think of it is if 'schedule' misses one, it forgets about it, if 'scheduleAtFixedRate' misses one, it puts in a make-up. – Affe Mar 1 '13 at 2:17
Timer TimerTask schedule scheduleAtFixedRate的更多相关文章
- 简单理解java中timer的schedule和scheduleAtFixedRate方法的区别
timer的schedule和scheduleAtFixedRate方法一般情况下是没什么区别的,只在某个情况出现时会有区别--当前任务没有来得及完成下次任务又交到手上. 我们来举个例子: 暑假到了老 ...
- Java中timer的schedule()和schedualAtFixedRate()函数的区别
本文主要讨论java.util.Timer的schedule(timerTask,delay,period)和scheduleAtFixedRate(timerTask,delay,period)的区 ...
- 线程 Timer TimerTask 计时器 定时任务 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Timer&TimerTask原理分析
转载地址,请珍惜作者的劳动成果,转载请注明出处:http://www.open-open.com/lib/view/open1337176725619.html 如果你使用Java语言进行开发,对于定 ...
- JDK Timer & TimerTask
目录 Timer & TimerTask Binary Heap Insert DELETE MIN PERFORMANCE LifeCycle Constructor MainLoop sc ...
- Timer的schedule和scheduleAtFixedRate方法的区别解析(转)
在java中,Timer类主要用于定时性.周期性任务 的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下 (1)sched ...
- Timer的schedule和scheduleAtFixedRate方法的区别解析
在java中,Timer类主要用于定时性.周期性任务 的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下 (1)sched ...
- Java Timer, TimerTask, Timer.Schedule
schedule的意思(时间表.进度表) timer.schedule(new TimerTask(){ void run()},0, 60*60*1000);timer.schedule(new M ...
- java timer timertask mark
其实就Timer来讲就是一个调度器,而TimerTask呢只是一个实现了run方法的一个类,而具体的TimerTask需要由你自己来实现,例如这样: 1 2 3 4 5 6 Timer timer = ...
随机推荐
- sqlserver收缩数据库
缩数据库文件(如果不压缩,数据库的文件不会减小 企业管理器--右键你要压缩的数据库--所有任务--收缩数据库--收缩文件 --选择日志文件--在收缩方式里选择收缩至XXM,这里会给出一个允许收缩 ...
- C/C++基础----动态内存
why 管理较难,忘记释放会内存泄漏,提早释放可能非法引用,重复释放. 为了更容易,更安全的使用动态内存,提供智能指针,其默认初始化保存一个空指针. what shared_ptr允许多个指针指向同一 ...
- python3学习笔记三(数字类型,字符串)
数字(Number)类型 有四种类型:整数.布尔型.浮点数和复数 int整数 bool布尔,如True float浮点数,1.23 complex复数,1+2j.1.2+2.3j 内置的 type() ...
- C++之单例类模板
单例类模板:一个类只能有一个对象,比如超市收银系统中清点物品的仪器只有一个 设计思路: 1.构造函数,拷贝构造函数必须设计为private,防止自己生成新的对象 2.且类的指针要设计为static类型 ...
- Java NIO系列教程(十一) Pipe
Java NIO 管道是2个线程之间的单向数据连接.Pipe有一个source通道和一个sink通道.数据会被写到sink通道,从source通道读取. 这里是Pipe原理的图示: 创建管道 通过Pi ...
- 开发框架-.Net:Learun(力软敏捷开发)
ylbtech-开发框架-.Net:Learun(力软敏捷开发) 1.返回顶部 2.返回顶部 1. 系统简介:(1)后台采用MVC+EF架构,前台使用Jquery+Bootstrap,界面美观大气 ...
- json--处理框架
1.Android 中的Json解析工具fastjson .序列化.反序列化 2.Android Gson的使用总结 可以处理含有内部类的类,或包含集合内部类的类: 3.Android-JSONToo ...
- mysql 8.0 错误The server requested authentication method unknown to the client
mysql 安装了最新版本8.0.11后创建用户并授权后,授权的用户连接数据库提示 The server requested authentication method unknown to the ...
- 十三篇系列:king转折点,wooga瓶颈,supercell营收结构
转自:http://gamerboom.com/archives/95125 十三篇系列:king的历史转折点,wooga的瓶颈,supercell的营收结构 第一篇 这句话In other word ...
- 俞敏洪:我创业24年感悟的3条CEO守则
转自:http://www.gamelook.com.cn/2017/10/306603 创业24年,他的创业经历被拍成了电影.在一次小范围聚会时,他说现实远比剧本要精彩.“如果让我一切从头开始,我会 ...