![](/img/trans.png)
[英]Exception propagation in java.util.concurrent.CompletableFuture
[英]What is the purpose of java.util.concurrent.CompletableFuture#allOf?
如果我有Collection<CompletableFuture<MyResult>>
,我希望将其转换为CompletableFuture<Collection<MyResult>>
。 所以转换后我只有一个未来,可以使用CompletableFuture
方法thenApply
在MyResult
集合上编写bussines逻辑,如thenApply
, thenAccept
等。但CompletableFuture#allOf
结果类型为Void
因此在调用它后我得到“无结果”。 例如,我无法检索(根据我的理解)返回的未来的任何结果,其中correspods为Collection<CompletableFuture<MyResult>>
。
我怀疑CompletableFuture#allOf
只是在收集完毕之后才返回Future。 所以我可以调用CompletableFuture#allOf(...).isDone
然后在循环转换Collection<CompletableFuture>
manually
(!)到CompletableFuture<Collection>
,我的假设是正确的吗?
如果您需要CompletableFuture<Collection<MyResult>>
作为结果,您可以使用https://github.com/spotify/completable-futures(potify-completlablefutures库)中的allAsList方法获取它。 CompletableFutures.allAsList(List<CompletableFuture<MyResult>>)
将为您提供CompletableFuture<List<MyResult>>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.