enter image description here即使在调用执行程序中断shutdownNow方法后,线程也不会停止。
线程调用功能正在while循环中运行,它正在检查中断标志。 我试过在一段时间后向正在运行的线程发送中断标志,但它仍然在执行。我想强制停止线程。 任何人都可以说出这种行为。 附加样本Java代码:什么是停止执行程序服务运行线程的优雅方法
public class TestExecutor {
static volatile int j = 1;
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(5);
for (int i = 1; i <= 10; ++i) {
Future<Map<String, List<String>>> abc = pool.submit(new Callable<Map<String, List<String>>>() {
volatile boolean abortFlag = false;
@Override
public Map<String, List<String>> call() throws Exception {
while(!abortFlag){
long start = System.currentTimeMillis();
for(int k=0; k < 10e4 ;k++){
abortFlag = abort();
System.out.println(Thread.currentThread().getName() +" " +abortFlag);
}
System.out.println("counter val is:" +Thread.currentThread().getName() +" : " +j++);
long end = System.currentTimeMillis();
System.out.println("time for one execution : " +" " +Thread.currentThread().getName() +" :" +(end-start));
return null;
}
return null;
}
private boolean abort() {
if(Thread.currentThread().isInterrupted()){
return true;
}else{
return false;
}
}
});
}
pool.shutdown();
try {
if (pool.awaitTermination(3000, TimeUnit.MILLISECONDS)) {
System.out.println("task completed");
} else {
System.out.println("Forcing shutdown...");
pool.shutdownNow();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Closed");
}
} `
的可能的复制[?如何停止执行程序线程池的在Java中执行(https://stackoverflow.com/questions/1562079/如何停止执行执行程序-java中的threadpool-in- – alain
这里工作正常。程序停止。 –
@JB:我已经发布了图片,也可以看到它在abort标志为true后仍然在运行。请参阅编辑。 –