ScheduledExecutorService和取消行为

时间:2013-10-31 23:43:42

标签: java multithreading concurrency scheduled-tasks

我正在努力为此找到一个满意的答案:

什么可能导致ScheduledFuture#cancel(false)返回false?基本上我正在创建一个像这样的新调度程序:

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
ScheduledFuture<?> schedulerStatus = scheduler.scheduleWithFixedDelay(this, 0L, 1L, TimeUnit.SECONDS);

在run方法中,我有以下内容:

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;
        }
    }
}

希望这段代码片段证明了cancel返回false的两个原因实际上是不可能的(注意这都是单线程)。由于取消在单个预定线程内被清楚地调用,因此任务既不完整也不能取消。

需要注意的一点是,取消返回false是间歇性的,并且在系统负载不足时发生。

还有什么想法?

1 个答案:

答案 0 :(得分:3)

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

来自Java默认Future实现使用的默认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;
}