我有一个返回 List
期货的方法
List<Future<O>> futures = getFutures();
现在我想等到所有 futures 都成功完成处理,或者 futures 返回输出的任何任务抛出异常。即使一个任务抛出异常,等待其他 futures 也是没有意义的。
简单的方法是
wait() {
For(Future f : futures) {
try {
f.get();
} catch(Exception e) {
//TODO catch specific exception
// this future threw exception , means somone could not do its task
return;
}
}
}
但是这里的问题是,例如,如果第 4 个 future 抛出异常,那么我将不必要地等待前 3 个 futures 可用。
如何解决这个问题?倒计时闩锁会以任何方式提供帮助吗?我无法使用 Future isDone
因为 java 文档说
boolean isDone()
Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will return true.
原文由 user93796 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以使用 CompletionService 在期货准备就绪后立即接收期货,如果其中之一抛出异常,则取消处理。是这样的:
我认为您可以进一步改进以取消任何仍在执行的任务,如果其中一个任务抛出错误。