gpt4 book ai didi

java - 无法使用 SingleThreadExecutor 取消正在运行的任务

转载 作者:行者123 更新时间:2023-12-01 15:52:07 29 4
gpt4 key购买 nike

我有实现 Runnable 的类:

public class Abc implements Runnable{
public synchronized void run(){
while(!(Thread.currentThread().isInterrupted() || someCondition())){
doSomething();
try{
this.wait(SOME_TIME);
} catch(InterruptedException ex){
logger.error("Ex",ex);
Thread.currentThread.interrupt();
}
}}}

将其提交到线程池后,我想通过中断取消它。

futureTask.cancel(true);

但是今天我的 Runnable 对象停止接收中断。我已注销中断状态: false。不会抛出 InterruptedException。有什么建议吗?

最佳答案

我的猜测是中断被 doSomething 消耗了或者 doSomething 被占用了(没有返回)

在这样的循环中使用 wait() 对我来说表明您有一个更适合使用并发库的控制流。您能否提供更多有关您在等待什么的详细信息?

顺便说一句,如果将 try/cath 移到循环之外,它会简化它。

关于java - 无法使用 SingleThreadExecutor 取消正在运行的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5814677/

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