如何组合 3 个或更多完成阶段?
2022-09-01 05:47:53
如果有2个完成阶段,我可以将它们与方法相结合:thenCombine
CompletionStage<A> aCompletionStage = getA();
CompletionStage<B> bCompletionStage = getB();
CompletionStage<Combined> combinedCompletionStage =
aCompletionStage.thenCombine(bCompletionStage, (aData, bData) -> combine(aData, bData));
如果我有 3 个或更多 FinishStages,我可以创建一个方法链,但我必须使用临时对象来传递结果。例如,下面是使用和从包中执行的解决方案:thenCombine
Pair
Triple
org.apache.commons.lang3.tuple
CompletionStage<A> aCompletionStage = getA();
CompletionStage<B> bCompletionStage = getB();
CompletionStage<C> cCompletionStage = getC();
CompletionStage<D> dCompletionStage = getD();
CompletionStage<Combined> combinedDataCompletionStage =
aCompletionStage.thenCombine(bCompletionStage, (Pair::of))
.thenCombine(cCompletionStage, (ab, c) ->
Triple.of(ab.getLeft(), ab.getRight(), c))
.thenCombine(dCompletionStage, (abc, d) ->
combine(abc.getLeft(), abc.getMiddle(), abc.getRight(), d));
有没有更好的方法来合并来自多个完成阶段的结果?