gpt4 book ai didi

java - CompletableFuture.allOf() 与 anyOf()

转载 作者:行者123 更新时间:2023-12-02 01:41:30 24 4
gpt4 key购买 nike

来自 javadoc,

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 给出任何一个要完成的 future 的结果,无论是否异常。在所有传递的 future(包括异常完成的 future)完成之前,allOf 才会完成。如果任何 future 异常完成,第一个异常会反射(reflect)在结果中。

基本上,anyOf 的异常将始终来自已完成的一个 future,而对于 allOf,它可能来自 any。

关于java - CompletableFuture.allOf() 与 anyOf(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54395446/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com