java如何中断执行器线程
中断执行器线程的正确方法是什么? 我有这个: 名为Worker的线程类,方法为:
public void run() {
while(!(Thread.currentThread().isInterrupted()){
System.out.println("work " + Thread.currentThread().getName() + ":" + Thread.currentThread().isInterrupted());
}
}
和主类,包括:
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
Worker worker = new Worker();
executorService.execute(worker);
我试图调用worker.interrupt();
或executorService.shutdownNow();
,但我的线程继续运行,isInterrupted()为false
# 1 楼答案
你能发布所有相关代码吗?根据你提供的信息,我无法重现你描述的行为。请参见下面一个按预期工作的SSCCE输出:
代码: