传递给 CompletableFuture.allOf() 的所有期货都会运行吗?

JSe*_*ser 6 java semantics completable-future

所以我有几个我想运行的期货,即使有些失败,我希望所有人都有机会运行。所以如果我这样做:

CompletableFuture.allOf(futures).join()
Run Code Online (Sandbox Code Playgroud)

会是这样吗?我的推理是,每个未来在其执行器中都有自己的队列作业,因此如果主线程没有先完成,所有未来都会运行。我的问题是我特别.join()关注.allOf()所以我的应用程序在运行所有内容之前不会结束

所以allOf()语义让我感到困惑:无论是否成功,当所有传递的期货都完成时,未来的回报是否会完成?或者,如果它看到一个失败而没有等待其余的,它会完成一个失败的未来吗?

编辑

为了进一步说明我的问题,.allOf行为是这样的:

Stream.of(futures).forEach(future -> {
  try {
    future.join()
  } catch (Throwable e) {
    //dont throw, we want to join the rest
  }
})
Run Code Online (Sandbox Code Playgroud)

或者它的行为是否如下所示:

Stream.of(futures).forEach(future -> {
  try {
    future.join()
  } catch (Throwable e) {
    throw e; //All other remaining .join() wont run
  }
})
Run Code Online (Sandbox Code Playgroud)

是哪个?第一种情况还是第二种情况?因为我想要第一种情况,这就是我暂时在代码中使用的情况,但allOf()如果可能的话,我想使用它,因为它更美观

谢谢!

Dee*_*pak 4

是的,每个未来都将独立尝试完成。

我认为您也在尝试了解控制在各种场景中是如何流动的。我想出了 4 个场景:

  1. 由于未处理的异常而导致失败的未来
  2. 应该使用completeExceptionally AND 显式标记为失败的Future 在其尾部有一个异常块。
  3. 应使用completeExceptionally AND 显式标记为失败的Future 尾部没有异常块。
  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();
Run Code Online (Sandbox Code Playgroud)

控制台上产生的输出是

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
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,如果 future 抛出未处理的错误(如情况 1 所示),如果它有一个exceptionally链接到其尾部的块,则应在该点处理异常

对于情况 2,如果 future 被标记为失败completeExceptionally,如果 future 有一个链接到其尾部的处理程序,则该exceptionally块应由该块处理

在情况3中,Future被标记为失败并且没有异常块,因此它将由exceptionally下一级的块处理,在这种情况下它是exceptionally的块allOf()

如您所见,案例 4 运行完成,并且无论其他 future 是否失败,消息都会打印在控制台上。