3

我试图为此找到一个令人满意的答案:

什么可能导致 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 是间歇性的,并且在系统处于负载状态时发生。

还有什么想法吗?

4

1 回答 1

3

false如果它已经完成执行或已经被取消,它看起来会返回。

FutureJava 的默认实现使用的默认实现的源代码Executor

boolean innerCancel(boolean mayInterruptIfRunning) {
    for (;;) {
        int s = getState();
        if (ranOrCancelled(s))
            return false;
        if (compareAndSetState(s, CANCELLED))
            break;
    }
    if (mayInterruptIfRunning) {
        Thread r = runner;
        if (r != null)
            r.interrupt();
    }
    releaseShared(0);
    done();
    return true;
}
于 2013-10-31T23:51:43.433 回答