CompletableFuture.allOf() 与 anyOf()
CompletableFuture.allOf() vs anyOf()
来自 javadocs,
AllOf()
If any of the given CompletableFutures complete exceptionally, then
the returned CompletableFuture also does so, with a
CompletionException holding this exception as its cause.
AnyOf()
if it completed exceptionally, the returned CompletableFuture also
does so, with a CompletionException holding this exception as its
cause.
这是否意味着当任何 CompletableFuture 抛出异常时,allOf() 和 anyOf() 的行为方式相同?
问题是短语
returned CompletableFuture also does so
出现在两种方法中,但没有明确提及何时发生异常。
不完全是。 anyOf
给出了任何一个期货的结果,无论是否异常。 allOf
在所有通过的期货完成之前不会完成,包括异常完成的期货。如果任何期货异常完成,第一个异常会反映在结果中。
基本上,anyOf
的异常将始终来自已完成的未来,而 allOf
可能来自任何一个。
来自 javadocs,
AllOf()
If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so, with a CompletionException holding this exception as its cause.
AnyOf()
if it completed exceptionally, the returned CompletableFuture also does so, with a CompletionException holding this exception as its cause.
这是否意味着当任何 CompletableFuture 抛出异常时,allOf() 和 anyOf() 的行为方式相同?
问题是短语
returned CompletableFuture also does so
出现在两种方法中,但没有明确提及何时发生异常。
不完全是。 anyOf
给出了任何一个期货的结果,无论是否异常。 allOf
在所有通过的期货完成之前不会完成,包括异常完成的期货。如果任何期货异常完成,第一个异常会反映在结果中。
基本上,anyOf
的异常将始终来自已完成的未来,而 allOf
可能来自任何一个。