futures.forEach(CompletableFuture::join) 并行运行所有任务?

futures.forEach(CompletableFuture::join) runs all tasks parallel?

我创建了多个可完成的期货:

CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> xxx);
CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> xxx);
List<CompletableFuture<Void>> futures = Lists.newArrayList(future1, future2);

当我在下面运行时,这两个future tasks 运行ning是并行的吗?一个未来抛出的异常会阻止另一个吗?

futures.forEach(CompletableFuture::join);

通过调用 CompletableFuture.runAsync(...) the tasks are submitted to the common ForkJoinPool。该池由 JVM 管理。

由于它是一个公共池,因此无法保证 made/can 任务 实际上 运行 并发或并行(其他任务可能会提交给池,或者池可能只有一个线程供其使用,从而导致任务的顺序执行)。但在最好的情况下,它们将被并行处理。

一项任务中的 Exception 不会对另一项任务产生任何影响(除非在任务中明确实施)。事实上,如果没有进一步的配置,Exception 将被默默地忽略(捕捉 Exceptions,一个 explicit UncaughtExceptionHandler can be set when constructing a ForkJoinPool). One can also figure out if a ComletableFuture has completed with an Exception by calling isDone() (to know if the task has been executed) and isCompletedExceptionally() (to know whether the task has completed with an Exception). The Exception itself can be obtained by calling get(),但应该注意 Exception在调用get().

时抛出,未返回