Java Timer
Java Timer 定时类,主要用来执行定时任务
Timer管理所有要执行的定时任务
TimerTask封装好的定时任务
常见的用法
MyTask myTask = new MyTask();
Timer timer = new Timer();
timer.schedule(myTask, new Date(), 1000);
void |
schedule(TimerTask task, long delay) |
Schedules the specified task for execution after the specified delay.
|
|---|---|---|
void |
schedule(TimerTask task, long delay, long period) |
Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
|
void |
schedule(TimerTask task, Date time) |
Schedules the specified task for execution at the specified time.
|
void |
schedule(TimerTask task, Date firstTime, long period) |
Schedules the specified task for repeated fixed-delay execution, beginning at the specified time.
|
void |
scheduleAtFixedRate(TimerTask task, long delay, long period) |
Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.
|
void |
scheduleAtFixedRate(TimerTask task,Date firstTime, long period) |
Schedules the specified task for repeated fixed-rate execution, beginning at the specified time.
|
摘自jdk,简单概括一下
schedule(TimerTask task, Date time):安排在指定的时间执行指定的任务。
schedule(TimerTask task, Date firstTime, long period) :安排指定的任务在指定的时间开始进行重复的固定延迟执行。
schedule(TimerTask task, long delay) :安排在指定延迟后执行指定的任务。
schedule(TimerTask task, long delay, long period) :安排指定的任务从指定的延迟后开始进行重复的固定延迟执行。
scheduleAtFixedRate(TimerTask task, Date firstTime, long period):安排指定的任务在指定的时间开始进行重复的固定速率执行。
scheduleAtFixedRate(TimerTask task, long delay, long period):安排指定的任务在指定的延迟后开始进行重复的固定速率执行。
cancel()取消所有定时任务
再来看下TimerTask
| Modifier and Type | Method | Description |
|---|---|---|
boolean |
cancel() |
Cancels this timer task.
|
abstract void |
run() |
The action to be performed by this timer task.
|
long |
scheduledExecutionTime() |
Returns the scheduled execution time of the most recent actual execution of this task.
|
只有run()是抽象方法,需要重写
cancel()表示取消该定时任务
scheduledExecutionTime()表示最近的定时任务的实际执行时间
schedule()有个缺点,如果之前的线程花费时间较长,那么相应的之后的线程的执行时间也会相应延长
scheduleAtFixedRate()可以避免上述问题,schedule()侧重时间间隔的稳定,而scheduleAtFixedRate()侧重执行频率的稳定,即便前一个线程没有执行完,而下一个线程到了该执行的时间,也会开始下一个线程
Java Timer的更多相关文章
- Java Timer触发定时器
XML: <!-- Java Timer定时 --> <!-- <bean id="shortUrlTask" class=" com.sprin ...
- Java:利用java Timer类实现定时执行任务的功能
一.概述 在java中实现定时执行任务的功能,主要用到两个类,Timer和TimerTask类.其中Timer是用来在一个后台线程按指定的计划来执行指定的任务.TimerTask一个抽象类,它的子类代 ...
- JAVA Timer定时器使用方法(二)
JAVA Timer 定时器测试 MyTask.java:package com.timer; import java.text.SimpleDateFormat;import java.util. ...
- Java定时任务:利用java Timer类实现定时执行任务的功能
一.概述 在java中实现定时执行任务的功能,主要用到两个类,Timer和TimerTask类.其中Timer是用来在一个后台线程按指定的计划来执行指定的任务. TimerTask一个抽象类,它的子类 ...
- JAVA Timer定时器使用方法
JAVA Timer 定时器测试 MyTask.java:package com.timer; import java.text.SimpleDateFormat;import java.util. ...
- java Timer定时器管理类
1.java timer类,定时器类.启动执行定时任务方法是timer.schedule(new RemindTask(), seconds*1000);俩参数分别是TimerTask子类,具体执行定 ...
- Java Timer TimerTask Example(java Timer的例子)
Java java.util.Timer is a utility class that can be used to schedule a thread to be executed at cert ...
- java Timer 定时每天凌晨1点执行任务
import java.util.TimerTask;/** * 执行内容 * @author admin_Hzw * */public class Task extends TimerTask { ...
- java timer 执行任务
1. 建立timer import java.util.Timer; import java.util.TimerTask; public class Start { public class Sta ...
随机推荐
- FreeMarker的空值运算符和逻辑运算符
1.空值处理运算符 如果你在模板中使用了变量但是在代码中没有对变量赋值,那么运行生成时会抛出异常.但是有些时候,有的变量确实是null,怎么解决这个问题呢? 判断某变量是否存在:“??” 用法为:va ...
- Linux网络管理-相关笔记【自用】
ISO/OSI七层模型应用层 APDU 应用层协议数据单元 越靠近用户表示层 PPDU 表示层协议数据单元会话层 SPDU 会话协 ...
- AWS 相关阅读
http://www.infoq.com/cn/articles/aws-s3-dive-in http://baike.baidu.com/link?url=Jo9OQmjxXZLxyhc6JvaD ...
- RAD Tokyo 10.2.2
TDialogService类 如果您是使用比较新的RAD Studio版本. 那么您应该开始改用FMX.DialogService程序单元中TDialogService类别的类别方法来取代以前的Me ...
- TFrame bug
delphi 10.1.2 工程里有很多fram 正确的工程文件dproj中fram的定义是 <DCCReference Include="Unit15frame.pas"& ...
- UI5-Fiori初学者导航
正文 你是UI5和Fiori的新手?来对地方了. 对我来说,今年是不得不“跟上时代”去提升自己ABAP世界以外的技术技能的困难的一年.幸运的是,有很多可免费获得的信息和课程可以帮你实现这个跳跃.不要等 ...
- JavaScript的 onclick 事件是如何调用jquery 方法的
看见个不错的问答,关于JavaScript的 onclick 事件是如何调用jquery 方法的,特此标注,链接如下:http://segmentfault.com/q/101000000033350 ...
- native.js 判断是否安装某app
例:是否安装微信 function isWeixin() { var UIApplication = plus.ios.importClass("UIApplication"); ...
- Erlang Error Records
1.No match of right hand value ... Erlang变量名需要以大写开头.
- 命令行下将磁盘从GPT转换为MBR
1.在系统提示无法安装的那一步,按住“shift+f10”,呼出“cmd”命令符 2.输入:diskpart,回车 进入diskpart 3.输入:list disk,回车 显示磁盘信息 4.输入:s ...