标签:
Java 实现定时器Timer
Timer是jdk中提供的一个定时器工具,使用的时候会在主线程之外起一个单独的线程执行指定的计划任务,可以指定执行一次或者反复执行多次。
TimerTask是一个实现了Runnable接口的抽象类,代表一个可以被Timer执行的任务。
实现Task
package com.usoft.timer;
import java.util.TimerTask;
public class Task extends TimerTask {
public void run() {
System.out.println("定时任务执行,taskId=" + this.hashCode());
}
}
测试Timer
package com.usoft.timer;
import java.util.Calendar;
import java.util.Timer;
public class TimeTaskTest {
public static void main(String[] args) throws InterruptedException {
Timer timer = new Timer();
timer.schedule(new Task(), 10 * 1000); //10 秒延迟执行
timer.schedule(new Task(), 60 * 1000, 10 * 1000);//60 秒延迟执行,每隔十秒执行一次
Calendar c = Calendar.getInstance();
c.add(Calendar.MILLISECOND, 1000 * 60);
timer.schedule(new Task(), c.getTime()); //当前时间的60秒后执行
timer.schedule(new Task(), c.getTime(), 10 * 1000);
timer.scheduleAtFixedRate(new Task(), c.getTime(), 10 * 1000);
timer.scheduleAtFixedRate(new Task(), 60 * 1000, 10 * 1000);
Thread.sleep(1000 * 120);
timer.cancel();//定时器停止运行
}
}
默认情况下,创建的timer线程会一直执行,主要有下面四种方式来终止timer线程:
调用timer的cancle方法
把timer线程设置成daemon线程,(new Timer(true)创建daemon线程),在jvm里,如果所有用户线程结束,那么守护线程也会被终止,不过这种方法一般不用。
当所有任务执行结束后,删除对应timer对象的引用,线程也会被终止。
调用System.exit方法终止程序
这种方式终止timer线程,jdk的实现比较巧妙,稍微说一下。
首先看cancle方法的源码:
复制代码
public void cancel() {
synchronized(queue) {
thread.newTasksMayBeScheduled = false;
queue.clear();
queue.notify(); // In case queue was already empty.
}
}
复制代码
没有显式的线程stop方法,而是调用了queue的clear方法和queue的notify方法,clear是个自定义方法,notify是Objec自带的方法,很明显是去唤醒wait方法的。
再看clear方法:
void clear() {
// Null out task references to prevent memory leak
for (int i=1; i<=size; i++)
queue[i] = null;
size = 0;
}
clear方法很简单,就是去清空queue,queue是一个TimerTask的数组,然后把queue的size重置成0,变成empty.还是没有看到显式的停止线程方法,回到最开始new Timer的时候,看看new Timer代码:
public Timer() {
this("Timer-" + serialNumber());
}
public Timer(String name) {
thread.setName(name);
thread.start();
}
看看这个内部变量thread:
/** * The timer thread. */ private final TimerThread thread = new TimerThread(queue);
不是原生的Thread,是自定义的类TimerThread.这个类实现了Thread类,重写了run方法,如下:
public void run() {
try {
mainLoop();
} finally {
// Someone killed this Thread, behave as if Timer cancelled
synchronized(queue) {
newTasksMayBeScheduled = false;
queue.clear(); // Eliminate obsolete references
}
}
}
最后是这个mainLoop方法,这方法比较长,
/**
* The main timer loop. (See class comment.)
*/
private void mainLoop() {
while (true) {
try {
TimerTask task;
boolean taskFired;
synchronized(queue) {
// Wait for queue to become non-empty
while (queue.isEmpty() && newTasksMayBeScheduled)
queue.wait();
if (queue.isEmpty())
break; // Queue is empty and will forever remain; die
// Queue nonempty; look at first evt and do the right thing
long currentTime, executionTime;
task = queue.getMin();
synchronized(task.lock) {
if (task.state == TimerTask.CANCELLED) {
queue.removeMin();
continue; // No action required, poll queue again
}
currentTime = System.currentTimeMillis();
executionTime = task.nextExecutionTime;
if (taskFired = (executionTime<=currentTime)) {
if (task.period == 0) { // Non-repeating, remove
queue.removeMin();
task.state = TimerTask.EXECUTED;
} else { // Repeating task, reschedule
queue.rescheduleMin(
task.period<0 ? currentTime - task.period
: executionTime + task.period);
}
}
}
if (!taskFired) // Task hasn‘t yet fired; wait
queue.wait(executionTime - currentTime);
}
if (taskFired) // Task fired; run it, holding no locks
task.run();
} catch(InterruptedException e) {
}
}
}
可以看到wait方法,之前的notify就是通知到这个wait,然后clear方法在notify之前做了清空数组的操作,所以会break,线程执行结束,退出。
通过调用三个参数的schedule方法实现,最后一个参数是执行间隔,单位毫秒。
这两个方法都是任务调度方法,他们之间区别是,schedule会保证任务的间隔是按照定义的period参数严格执行的,如果某一次调度时间比较长,那么后面的时间会顺延,保证调度间隔都是period,而scheduleAtFixedRate是严格按照调度时间来的,如果某次调度时间太长了,那么会通过缩短间隔的方式保证下一次调度在预定时间执行。举个栗子:你每个3秒调度一次,那么正常就是0,3,6,9s这样的时间,如果第二次调度花了2s的时间,如果是schedule,就会变成0,3+2,8,11这样的时间,保证间隔,而scheduleAtFixedRate就会变成0,3+2,6,9,压缩间隔,保证调度时间。
每一个Timer仅对应唯一一个线程。
Timer不保证任务执行的十分精确。
Timer类的线程安全的。
==============================END==============================
标签:
原文地址:http://my.oschina.net/xinxingegeya/blog/492094