java invokeall 阻塞_ExecutorService.invokeAll并关闭
所以我有一些Callable任務(wù), sensitive to interruptions ,我使用invokeAll提交給ExecutorService . 從另一個(gè)方法開始5秒后,我調(diào)用executorService.shutdownNow,然后調(diào)用awaitTermination,返回true,所以看起來都很好 . 問題是執(zhí)行者永遠(yuǎn)不會(huì)終止 .
Due to logging I know that each one of my tasks finished. 然而,當(dāng)i等于執(zhí)行程序的線程數(shù)時(shí),invokeAll仍然在f.get上阻塞:
The following code is obtained from AbstractExecutorService 一些日志記錄 .
@Override
public List> invokeAll(Collection extends Callable> tasks) throws InterruptedException {
if (tasks == null) throw new NullPointerException();
ArrayList> futures = new ArrayList>(tasks.size());
boolean done = false;
try {
List> list = new ArrayList>();
for (Callable t : tasks) {
list.add(t);
RunnableFuture f = newTaskFor(t);
futures.add(f);
execute(f);
}
for (int i = 0, size = futures.size(); i < size; i++) {
Future f = futures.get(i);
if (!f.isDone()) {
log.info("Future %s is not done!. Task %s", i, list.get(i));
try {
log.info("Get from future %s", i);
// NEXT LINE BLOCKS FOR i= NUMBER OF THREADS
f.get();
log.info("Got result from future %s", i);
} catch (CancellationException ignore) {
} catch (ExecutionException ignore) {
}
}
}
log.info("Obtained all!");
done = true;
return futures;
} finally {
if (!done) for (int i = 0, size = futures.size(); i < size; i++)
futures.get(i).cancel(true);
}
}
我不想在關(guān)機(jī)時(shí)使用invokeAll嗎?我猜不是,畢竟他們?cè)谕粋€(gè) class . 只有當(dāng)i =執(zhí)行程序的線程數(shù)時(shí),為什么它會(huì)被阻塞?
總結(jié)
以上是生活随笔為你收集整理的java invokeall 阻塞_ExecutorService.invokeAll并关闭的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Win7如何美化输入法图标色彩(iSli
- 下一篇: Win7如何启用监控功能