gpt4 book ai didi

java - 在另一个 CompletableFuture 的结果上应用多个 CompletableFuture 的最佳方法是什么?

转载 作者:行者123 更新时间:2023-11-30 02:06:06 27 4
gpt4 key购买 nike

让我们举个例子:我们有四种方法:

CompletableFututre<Void> loadAndApply(SomeObject someObject);
CompletableFuture<SomeData> loadData();
A processA(SomeData data);
B processB(SomeData data);

loadAndApply 结合了所有其他方法。 loadData长时间获取数据。然后我们将 someObject.A 设置为运行 processA(data) 的结果,并将 someObject.B 设置为运行 processB(data) 的结果)我们不能同时应用 processAprocessB,因为 processA 只能在 swingExecutor 上运行,并且processB 只能在 backgroundExecutor 上运行。

所以我的问题是:我们能否以某种好看的方式链接所有这些方法?

目前我像这样启动它们:

CompletableFututre<Void> loadAndApply(SomeObject someObject) {
return loadData()
.thenApplyAsync(data -> { someObject.setA(processA(data)); return data; }, swingExecutor)
.thenAcceptAsync(data -> someObject.setB(processB(data)), backgroundExecutor);
}

有没有比 applyAsync 看起来更好的方法,它实际上不对给定对象应用任何内容,只是为下一个将来返回它?

最佳答案

您可以使用 CompletionStage.thenCompose(Function) 来做到这一点与 CompletableFuture.allOf(CompletableFuture...) 结合。 Function 的通用签名由 thenCompose 使用是:Function<? super T, ? extends CompletionStage<U>> .

public CompletableFuture<Void> loadAndApply(SomeObject object) {
return loadData().thenCompose(data ->
CompletableFuture.allOf(
CompletableFuture.runAsync(() -> object.setA(processA(data)), swingExecutor),
CompletableFuture.runAsync(() -> object.setB(processB(data)), backgroundExecutor)
) // End of "allOf"
); // End of "thenCompose"
} // End of "loadAndApply"

这还有一个额外的好处。在代码中,您当前使用 thenAcceptAsync舞台还需等待thenApplyAsync执行之前要完成的阶段。当使用上述两者时 setAsetB将在各自的执行器中同时运行。

为了方便起见,这里是 allOf 的 Javadoc :

Returns a new CompletableFuture that is completed when all of the given CompletableFutures complete. If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so, with a CompletionException holding this exception as its cause. Otherwise, the results, if any, of the given CompletableFutures are not reflected in the returned CompletableFuture, but may be obtained by inspecting them individually. If no CompletableFutures are provided, returns a CompletableFuture completed with the value null.

Among the applications of this method is to await completion of a set of independent CompletableFutures before continuing a program, as in: CompletableFuture.allOf(c1, c2, c3).join();.

...以及 thenCompose 的 Javadoc :

Returns a new CompletionStage that is completed with the same value as the CompletionStage returned by the given function.

When this stage completes normally, the given function is invoked with this stage's result as the argument, returning another CompletionStage. When that stage completes normally, the CompletionStage returned by this method is completed with the same value.

To ensure progress, the supplied function must arrange eventual completion of its result.

This method is analogous to Optional.flatMap and Stream.flatMap.

See the CompletionStage documentation for rules covering exceptional completion.

注意:CompletableFuture ,它实现 CompletionStage ,覆盖thenCompose但使返回类型更加具体(返回 CompletableFuture 而不是 CompletionStage )

关于java - 在另一个 CompletableFuture 的结果上应用多个 CompletableFuture 的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51324304/

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