我有一个已链接的CompletableFuture呼叫
void handle(ParamA paramA) {
CompletableFuture.runAsync(() -> methodA(paramA))
.thenApply(resultA -> methodB(paramA, resultA))
.thenApply(resultB -> methodC(resultA, resultB));
}
问题在于,对于methodC,它需要在上一次调用中生成的参数,但是参数超出范围,我如何访问resultA?
答案 0 :(得分:0)
您可以这样做
CompletableFuture cfA = CompletableFuture.runAsync(() -> methodA(paramA));
CompletableFuture cfB = cfA.thenApply(resultA -> methodB(paramA, resultA));
cfA.thenCombine(cfB, (resultA, resultB) -> methodC(resultA, resultB));