我想等待我所有的线程都被执行,然后才继续。
ExecutorService es = Executors.newFixedThreadPool(3);
List<Callable<Void>> calls = new LinkedList<>();
Arrays.stream(new int[]{1,2,3})
.forEach( i -> calls.add(() -> {
someFunc(i);
return null;
}));
try {
es.invokeAll(calls);
} catch (InterruptedException e) {
e.printStackTrace();
}
es.shutdown();
System.out.println(es.isTerminated()); //prints false
System.out.println(es.isShutdown()); //prints true
为什么我在这两种情况下都没有得到 true
?
我的所有线程都已成功终止。
最佳答案
查看 isTerminated
的文档突出了区别:
Returns
true
if all tasks have completed following shut down. Note thatisTerminated()
is nevertrue
unless eithershutdown()
orshutdownNow()
was called first.@return
true
if all tasks have completed following shut down
因此在您的情况下,这意味着执行程序已关闭但您的任务未完成。
关于java - 执行程序终止和关闭之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47078369/