【发布时间】:2014-09-25 12:20:12
【问题描述】:
比较 JavaDoc 与真实行为和源代码的 TimerTask cancel() 方法语义让我有些不知所措。
JavaDoc 关于返回值的说法:
如果任务被安排为一次性执行并且已经运行,或者如果任务从未被安排,或者如果任务已经被取消,则返回 false。
但是,如果任务尚未运行,但查看代码,可能会返回 false,但 将 运行。我在看 java.util.Timer 代码(mainLoop() 方法):
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();
似乎 cancel() 可以在最后一个 if 之前轻松调用,因为 TimerTask cancel() 代码显然只是设置了任务状态不与任务的 run() 同步:
public boolean cancel() {
synchronized(lock) {
boolean result = (state == SCHEDULED);
state = CANCELLED;
return result;
}
}
所以,从我的角度来看,在上面给出的 JavaDoc 中的“松散”定义似乎确实很严格:
简单地说,如果这个方法阻止了一个或多个计划执行的发生,则返回 true。
所以,再一次,看起来像 false 作为 cancel() 方法的结果可能意味着任务没有被执行,但会被执行。
请确认我的想法或告诉我我哪里错了。
谢谢!
【问题讨论】: