我试图为此找到一个令人满意的答案:
什么可能导致 ScheduledFuture#cancel(false) 返回 false?本质上,我正在创建一个新的调度程序,如下所示:
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
ScheduledFuture<?> schedulerStatus = scheduler.scheduleWithFixedDelay(this, 0L, 1L, TimeUnit.SECONDS);
在运行方法中,我有以下内容:
public void run() {
// Do some logic
schedulerStatus.cancel(false);
}
我看过JavaDoc但它并没有真正说明任何帮助,
“或因其他原因无法取消”
因此,任何有助于了解这里到底发生了什么都会很棒!
谢谢
更新
为了进一步扩展我上面的例子:
private final ScheduledExecutorService scheduler;
private volatile ScheduledFuture<?> schedulerStatus;
@Inject
public IndexChangeManagerImpl(IndexChangeMonitor monitor, IndexChangeObservable observable) {
this.monitor = monitor;
scheduler = Executors.newScheduledThreadPool(1);
}
@Override
public void init() {
if (schedulerStatus == null || schedulerStatus.isDone()) {
// Kick of the scheduler to attempt to initiate the listener every second.
schedulerStatus = scheduler.scheduleWithFixedDelay(this, 0L, 1L, TimeUnit.SECONDS);
}
}
@Override
public void run() {
try {
// Before starting the monitor, ensure it is not currently running.
monitor.shutdown();
// Initiate the monitor.
monitor.start();
// Listener has been established, no more attempts necessary.
schedulerStatus.cancel(false);
lastLog = 0;
} catch (ChangeMonitorException sfE) {
long now = System.currentTimeMillis();
if (lastLog == 0 || now - lastLog > 60000) {
// Log every 60 seconds.
DEBUG.error("Error attempting to initiate index change monitor.", sfE);
lastLog = now;
}
}
}
希望这段代码片段证明了取消返回 false 的两个原因实际上是不可能的(注意这都是单线程的)。由于在单个计划线程中调用了取消,因此任务既不完整也不可能已经被取消。
需要注意的一件事是,取消返回 false 是间歇性的,并且在系统处于负载状态时发生。
还有什么想法吗?