今天修复了一个定时任务,看了点源码,记录一下。 主要是Timer的6个方法的分析,首先先看一下这6个方法都调用的方法sched:
private void sched(TimerTask task, long time, long period) {
if (time < 0)
throw new IllegalArgumentException("Illegal execution time.");
synchronized(queue) {
if (!thread.newTasksMayBeScheduled)
throw new IllegalStateException("Timer already cancelled.");
synchronized(task.lock) {
if (task.state != TimerTask.VIRGIN)
throw new IllegalStateException(
"Task already scheduled or cancelled");
task.nextExecutionTime = time;
task.period = period;
task.state = TimerTask.SCHEDULED;
}
queue.add(task);
if (queue.getMin() == task)
queue.notify();
}
}
当调用sched方法时, 1.period周期为正,则任务被安排为重复执行;如果周期为零,则任务被安排为一次性执行;
2.当time时间为过去时间时,如果period周期为正,则按照‘过去时间time+period周期’执行;如果周期为负, 按照 ‘当前时间+period周期’执行,即执行当前时间以后的任务。
可以参考源码如下:
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
//这里判断period,为正返回executionTime + task.period
//为负返回currentTime - task.period
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) {
}
}
}
}
明白了上面的原理,接下来就比较容易理解6个方法了。
public void schedule(TimerTask task, long delay) {
if (delay < 0)
throw new IllegalArgumentException("Negative delay.");
sched(task, System.currentTimeMillis()+delay, 0);
}
sched、mainLoop方法源码可以看出:
- period周期为0,delay延迟后,执行一次
public void schedule(TimerTask task, Date time) {
sched(task, time.getTime(), 0);
}
sched、mainLoop方法源码可以看出:
- time为过去时间,直接执行;
- time为未来时间,等到时间达time时执行。
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);
}
sched、mainLoop方法源码可以看出:
- 延迟delay,开始执行
public void schedule(TimerTask task, Date firstTime, long period) {
if (period <= 0)
throw new IllegalArgumentException("Non-positive period.");
sched(task, firstTime.getTime(), -period);
}
sched、mainLoop方法源码可以看出:
- firstTime为过去时间,则执行当前时间以后的任务;
- firstTime为未来时间,则等到到达firstTime时间开始执行任务。
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);
}
sched、mainLoop方法源码可以看出:
- 延迟delay时间后,开始执行任务
public void scheduleAtFixedRate(TimerTask task, Date firstTime,
long period) {
if (period <= 0)
throw new IllegalArgumentException("Non-positive period.");
sched(task, firstTime.getTime(), period);
}
sched、mainLoop方法源码可以看出:
- firstTime如果为过去时间,则从过去时间开始执行,并以period为周期一直执行;
- firstTime如果去未来时间,则等到firstTime到达时,开始执行,并以period为周期一直执行。
- 这里可以看出和4的区别,当firstTime为过去时间时,6补了从设定时间开始的任务,而4只管执行现在及以后的任务。
- 如果firstTime为未来时间,4、6没有区别