gpt4 book ai didi

java - ScheduledExecutorService 和取消行为

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:26 27 4
gpt4 key购买 nike

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

什么可能导致 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但它并没有真正说出任何有用的东西,

"or could not be cancelled for some other reason"

因此,如果能帮助理解这里到底发生了什么,那就太好了!

谢谢

更新

进一步扩展我上面的例子:

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 的两个原因实际上是不可能的(注意这都是单线程的)。由于在单个计划线程内调用取消,显然任务既未完成也无法取消。

需要注意的是,cancel 返回 false 是间歇性的,发生在系统负载不足的情况下。

还有什么想法吗?

最佳答案

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

来自 Java 默认 Executor 实现所使用的默认 Future 实现的源代码:

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

关于java - ScheduledExecutorService 和取消行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19718535/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com