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. [Android]Android布局优化之<include />

    转载请标明:转载于http://www.cnblogs.com/Liuyt-61/p/6602891.html -------------------------------------------- ...

  2. Notes on Noise Contrastive Estimation and Negative Sampling

    Notes on Noise Contrastive Estimation and Negative Sampling ## 生成负样本 在常见的关系抽取应用中,我们经常需要生成负样本来训练一个好的系 ...

  3. ROS进阶学习笔记(11)- Turtlebot Navigation and SLAM - ROSMapModify - ROS地图修改

    ROS进阶学习笔记(11)- Turtlebot Navigation and SLAM - 2 - MapModify地图修改 We can use gmapping model to genera ...

  4. CPU UsageTimes Profile (cpu=times)

    HPROF工具能搜集CPU使用信息通过注入代码到每个方法进入点和退出点.因此能够统计方法真实调用次数和花费的时间. 它使用BCI(Byte Code Injection),所以比cpu=samples ...

  5. 20.纯 CSS 为母亲节创作一颗像素画风格的爱心

    原文地址:https://segmentfault.com/a/1190000014837536 感想: 网格grid 又来了: fr : (剩余空间长度)单位, 1.当(50px,nfr),nfr代 ...

  6. WebService简单教程

    一.简介 1.什么是WebService? WebService是一个SOA(面向服务的编程)的架构,它是不依赖于语言,不依赖于平台,可以实现不同的语言间的相互调用,通过Internet进行基于Htt ...

  7. exe加载DLL的时候会有一系列的搜索路径

    假如安全DLL搜索模式启用,搜索顺序如下: 1. 应用程序所在的路径 2. Windows SYSTEM目录.通过调用GetSystemDirectory函数可以获取这个目录的路径. 3. 16位系统 ...

  8. ASPxCallback组件(珍藏版)

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs ...

  9. static,final的用法

    static的用法:修鉓符,修鉓属性,方法,代码块a1.修鉓属性:该属性是一个静态的属性,叫类的成员(没有static修鉓的属性叫实例的成员,调用时用:对象名.属性),调用:类名.属性.a2.修鉓方法 ...

  10. 16_虚拟dom和dom diff算法

    虚拟dom的作用:是为了减少操作真实的dom 初始化显示界面的过程: 1.创建虚拟dom树——>真实dom树——>绘制页面显示 更新界面的过程: 2.绘制页面显示——>setStat ...