java定时操作之Timer和TimerTas(四)

2014-11-24 01:45:17 · 作者: · 浏览: 2
ght 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) {

}

}

}

}

3.Timer类的主体和主要对外提供的方法

Java代码

import java.util.*;

import java.util.Date;

public class Timer {

// 定时任务队列

private TaskQueue queue = new TaskQueue();

// 计时器线程

private TimerThread thread = new TimerThread(queue);

private Object threadReaper = new Object() {

protected void finalize() throws Throwable {

synchronized (queue) {

thread.newTasksMayBeScheduled = false;

queue.notify(); // In case queue is empty.

}

}

};

// ID号作为线程的ID

private static int nextSerialNumber = 0;

private static synchronized int serialNumber() {

return nextSerialNumber++;

}

public Timer() {

this("Timer-" + serialNumber());

}

// 创建一个新计时器,可以指定其相关的线程作为守护程序运行。

public Timer(boolean isDaemon) {

this("Timer-" + serialNumber(), isDaemon);

}

public Timer(String name) {

thread.setName(name);

thread.start();

}

// 创建一个新计时器,其相关的线程具有指定的名称,并且可以指定作为守护程序运行。

public Timer(String name, boolean isDaemon) {

thread.setName(name);

thread.setDaemon(isDaemon); 织梦内容管理系统

thread.start();

}

// 安排在指定延迟后执行指定的任务。时间单位毫秒

public void schedule(TimerTask task, long delay) {

if (delay < 0)

throw new IllegalArgumentException("Negative delay.");

sched(task, System.currentTimeMillis() + delay, 0);

}

// 安排在指定的时间执行指定的任务。

public void schedule(TimerTask task, Date time) {

sched(task, time.getTime(), 0);

}

// 安排指定的任务从指定的延迟后开始进行重复的固定延迟执行。

public void schedule(TimerTask task, long delay, long period) {

if (delay < 0)

throw new IllegalArgumentException("Negative delay.");

if (period <= 0)

throw new IllegalArgumentException("Non-positive period.");

sched(task, System.currentTimeMillis() + delay, -period);

}

// 安排指定的任务在指定的时间开始进行重复的固定延迟执行。

public void schedule(TimerTask task, Date firstTime, long period) {

if (period <= 0)

throw new IllegalArgumentException("Non-positive period.");

sched(task, firstTime.getTime(), -period);

}

// 安排指定的任务在指定的延迟后开始进行重复的固定速率执行。

public void scheduleAtFixedRate(TimerTask task, long delay, long period) {

if (delay < 0)

throw new IllegalArgumentException("Negative delay.");

if (period <= 0)

throw new IllegalArgumentException("Non-positive period.");

sched(task, System.currentTimeMillis() + delay, period);

}

// 安排指定的任务在指定的时间开始进行重复的固定速率执行。

public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period) {

if (period <= 0)

throw new IllegalArgumentException("Non-positive period.");

sched(task, firstTime.getTime(), period);

}

private void sched(Ti