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 ...
随机推荐
- js调用打印机打印
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- sql server无log ldf日志文件附件mdf数据库重新生成ldf日志文件
CREATE DATABASE TestDB ON ( FILENAME ='D:\TestDB.mdf' --路径 ) for ATTACH_REBUILD_LOG
- <转载>Bootstrap 入门教程 http://www.cnblogs.com/ventlam/archive/2012/05/28/2520703.html 系列
Bootstrap建立了一个响应式的12列格网布局系统,它引入了fixed和fluid-with两种布局方式.我们从全局样式(Global Style),格网系统(Grid System),流式格网( ...
- 白鹭引擎 - 矢量绘图 ( graphics )
class Main extends egret.DisplayObjectContainer { /** * Main 类构造器, 初始化的时候自动执行, ( 子类的构造函数必须调用父类的构造函数 ...
- jsp grid can not be used in this ('quirks') mode
设置: <!--设置IE文档模式 --> <meta http-equiv="X-UA-Compatible" content="IE=9" ...
- C# 中的 ConfigurationManager类引用方法
c#添加了Configuration;后,竟然找不到 ConfigurationManager 这个类,后来才发现:虽然引用了using System.Configuration;这个包,但是还是不行 ...
- JVM虚拟机宕机_java.lang.OutOfMemoryError: unable to create new native thread
原因:当前用户的系统最最大程序数数已达到最大值,使用ulimit -u可以看到是1024 解决办法:在当前用户下使用ulimit -u 65535 然后再执行jsp,一切ok 功能说明:控 ...
- Windows下卸载Apache、Mysql
卸载Apache 1. 停止服务 2.以管理员身份打开命令环境 3. 删除Apache文件目录 卸载Mysql 一.在控制面板,卸载MySQL的所有组件控制面板——>所有控制面板项——>程 ...
- Duboo 与springboot整合
https://github.com/apache/incubator-dubbo-spring-boot-project 当采用properties方式时,可以用下方的注解 1.pom <de ...
- 深度学习原理与框架-Tensorflow基本操作-实现线性拟合
代码:使用tensorflow进行数据点的线性拟合操作 第一步:使用np.random.normal生成正态分布的数据 第二步:将数据分为X_data 和 y_data 第三步:对参数W和b, 使用t ...