因此,我有一些要运行的期货,即使有些失败,我也希望所有人都有机会运行。所以,如果我这样做:
CompletableFuture.allOf(futures).join()
会是这样吗?我的理由是,每个未来都将在其执行程序中有其自己的可查询的工作,因此,只要主线程没有首先完成,一切都会运行。我的问题是我专门在.join()
上.allOf()
,所以我的应用程序在运行所有内容之前不会结束
所以allOf()
的语义使我感到困惑:如果所有成功通过的期货都完成了,则期货返回是否会完成而不管其成功与否?还是如果看到一个失败而没有等待其余的失败,它将完成一个失败的未来吗?
编辑
为进一步说明我的问题,.allOf
的行为是否像这样:
Stream.of(futures).forEach(future -> {
try {
future.join()
} catch (Throwable e) {
//dont throw, we want to join the rest
}
})
或者它的行为如下:
Stream.of(futures).forEach(future -> {
try {
future.join()
} catch (Throwable e) {
throw e; //All other remaining .join() wont run
}
})
是哪个?第一种还是第二种情况?由于我希望第一种情况就是我暂时在代码中使用的,但是我想尽可能使用allOf()
,因为它更美观
谢谢!
答案 0 :(得分:3)
是的,每个未来都将独立尝试完成。
我认为您还试图了解控件在各种情况下的流动方式。我提出了4种情况:
//CASE 1
// A future that shall fail due to an unandled exception in its run
// and has an exceptionally block at its tail
CompletableFuture<Void> unhandledFailureFutureWithExceptionHandler =
CompletableFuture.runAsync(() -> {
throw new RuntimeException("Exception in unhandledFailureFutureWithExceptionHandler");
});
unhandledFailureFutureWithExceptionHandler = unhandledFailureFutureWithExceptionHandler
.exceptionally(throwable -> {
// Handling exception for this future
// HANDLING POINT 1
System.out.println("Handling exception at HANDLING POINT FOR CASE 1,
failure message is : " + throwable.getMessage());
return null;
});
//CASE 2
//A future that shall fail and has an exceptionally block at its tail
CompletableFuture<Void> failedFutureWithExceptionHandler = new CompletableFuture<>();
failedFutureWithExceptionHandler.completeExceptionally(
new RuntimeException("Exception in failedFutureWithExceptionHandler")
);
failedFutureWithExceptionHandler = failedFutureWithExceptionHandler.exceptionally(throwable -> {
// Handling exception for this future
// HANDLING POINT 2
System.out.println("Handling exception at HANDLING POINT FOR CASE 2,
failure message is : " + throwable.getMessage());
return null;
});
//CASE 3
//A future that shall fail and has no exceptionally block at its tail
CompletableFuture<Void> failedFutureWithoutExceptionHandler = new CompletableFuture<>();
failedFutureWithoutExceptionHandler.completeExceptionally(
new RuntimeException("Exception in failedFutureWithoutExceptionHandler")
);
//CASE 4
//A future that shall succeed and print a message to console
CompletableFuture<Void> successFuture = CompletableFuture.runAsync(() ->
System.out.println("CASE 4 : Running successFuture")
);
CompletableFuture.allOf(unhandledFailureFutureWithExceptionHandler,
failedFutureWithExceptionHandler, failedFutureWithoutExceptionHandler, successFuture)
.exceptionally(throwable -> {
// Handling exception if ANY of the futures that did not have its own exceptionally block
// In this case the exception of `failedFutureWithoutExceptionHandler` will be handled here
// HANDLING POINT 3
System.out.println("Handling exception at HANDLING POINT FOR CASE 3,
failure message is : " + throwable.getMessage());
return null;
}).join();
控制台上产生的输出是
Handling exception at HANDLING POINT FOR CASE 1, failure message is : java.lang.RuntimeException: Exception in unhandledFailureFutureWithExceptionHandler
Handling exception at HANDLING POINT FOR CASE 2, failure message is : Exception in failedFutureWithExceptionHandler
CASE 4 : Running successFuture
Handling exception at HANDLING POINT FOR CASE 3, failure message is : java.lang.RuntimeException: Exception in failedFutureWithoutExceptionHandler
如您所见,将来是否会像情况1一样抛出未处理的错误,如果它的尾部有一个exceptionally
块,那么应该在该时刻处理异常
对于情况2,如果将来用completeExceptionally
标记为失败,如果将来有一个链接到其尾部的处理程序,则exceptionally
块应由该块处理< / p>
在情况3中,未来被标记为失败,并且没有异常块,因此应由下一级的exceptionally
块来处理,在这种情况下,它是exceptionally
allOf()
的块。
如您所见,案例4即将完成,并且无论其他期货是否出现故障,消息都会在控制台上显示。