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的更多相关文章

  1. Java Timer触发定时器

    XML: <!-- Java Timer定时 --> <!-- <bean id="shortUrlTask" class=" com.sprin ...

  2. Java:利用java Timer类实现定时执行任务的功能

    一.概述 在java中实现定时执行任务的功能,主要用到两个类,Timer和TimerTask类.其中Timer是用来在一个后台线程按指定的计划来执行指定的任务.TimerTask一个抽象类,它的子类代 ...

  3. JAVA Timer定时器使用方法(二)

    JAVA  Timer 定时器测试 MyTask.java:package com.timer; import java.text.SimpleDateFormat;import java.util. ...

  4. Java定时任务:利用java Timer类实现定时执行任务的功能

    一.概述 在java中实现定时执行任务的功能,主要用到两个类,Timer和TimerTask类.其中Timer是用来在一个后台线程按指定的计划来执行指定的任务. TimerTask一个抽象类,它的子类 ...

  5. JAVA Timer定时器使用方法

    JAVA  Timer 定时器测试 MyTask.java:package com.timer; import java.text.SimpleDateFormat;import java.util. ...

  6. java Timer定时器管理类

    1.java timer类,定时器类.启动执行定时任务方法是timer.schedule(new RemindTask(), seconds*1000);俩参数分别是TimerTask子类,具体执行定 ...

  7. 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 ...

  8. java Timer 定时每天凌晨1点执行任务

    import java.util.TimerTask;/** * 执行内容 * @author admin_Hzw * */public class Task extends TimerTask {  ...

  9. java timer 执行任务

    1. 建立timer import java.util.Timer; import java.util.TimerTask; public class Start { public class Sta ...

随机推荐

  1. uva-270-排序

    题意:很多个点,问,最多有多少个点在同一条直线上 #include <algorithm> #include <iostream> #include <string> ...

  2. 【转】R语言 RStudio快捷键

    链接地址  http://blog.sina.com.cn/s/blog_403aa80a0101ar8q.html 控制台 功能 Windows & Linux Mac 移动鼠标到控制台 C ...

  3. hive压缩

    1. 常用  rcfile + gzip parquet + snappy 2. 压缩比,参考 TextFile默认格式,加载速度最快,可以采用Gzip进行压缩,压缩后的文件无法split,即并行处理 ...

  4. add_featurelayer_to_map

    var jsonFS = { "geometryType": "esriGeometryPolygon", "features": [ { ...

  5. 【3-24】css样式表分类、选择器、样式属性

    一.css样式表分类: (一)内联样式表:代码写在标签内的样式表  控制精确 代码重用性差  优先级最高 格式:<p style="样式属性">内容</p> ...

  6. 34. Win7_x64安装oracle11g出现DIM-00019

    Win7_x64安装oracle11g出现如下异常处理方式 DIM-00019:创建服务时出错. O/S-ERROR:(OS 1726)远程过程调用失败. 点击弹出的错误框上的确定按钮后,继续弹出错误 ...

  7. 浅谈 CAS

    CAS: CAS:Compare and Swap, 翻译成比较并交换. java.util.concurrent包中借助CAS实现了区别于 synchronized 同步锁的一种乐观锁. CAS应用 ...

  8. jquery事件绑定与事件委托

    //事件绑定简写形式 $(".div2 button").click(function () { $(".div1").scrollTop(0) }) //写全 ...

  9. python字符串的基本用法

    var1 = "hello word"var2 = "runootab"print var2.capitalize()#首字母大写print (var2.cou ...

  10. text-align: justify;浏览器、安卓手机不兼容问题

    https://segmentfault.com/a/1190000013146385