我的同事喜欢的Java8编码风格是一直链接异步调用,例如CompletionStagesomeMethod(){returndoSomething().thenCompose(a->{//...returnb;}).thenCompose(b->{//...returnc;}).thenCompose(c->{//...returnd;}).thenApply(d->{//...returne;});}我有类似上面的内容,但还有一个额外的挑战:我需要在后面的lambda中记忆在一些lambda中检索到的值。例如,CompletionStagesomeMethod(){returndoS
我遇到了Java8CompletableFuturethenCompose方法的奇怪行为。我有两个测试,只是执行顺序不同。这两个测试都模拟了thenCompose中生成的CompletableFuture中的失败。@TestpublicvoidcompletedAfter(){CompletableFuturefuture1=newCompletableFuture();CompletableFuturefuture2=newCompletableFuture();future1.thenCompose(x->future2).whenComplete((r,e)->System.ou
我想在另一个CompletableFuture完成后执行一个CompletableFuture,无论第一个是否异常完成(.thenCompose()仅在执行正常完成时运行)。例如:CompletableFuture.supplyAsync(()->1L).whenComplete((v,e)->CompletableFuture.runAsync(()->{try{Thread.sleep(1000);System.out.println("HERE");}catch(InterruptedExceptionexc){return;}})).whenComplete((v,e)->Sy
我无法理解thenApply和thenCompose之间的区别。那么,有人可以提供一个有效的用例吗?来自Java文档:thenApply(Functionfn)ReturnsanewCompletionStagethat,whenthisstagecompletesnormally,isexecutedwiththisstage'sresultastheargumenttothesuppliedfunction.thenCompose(Function>fn)ReturnsanewCompletionStagethat,whenthisstagecompletesnormally,is
我无法理解thenApply和thenCompose之间的区别。那么,有人可以提供一个有效的用例吗?来自Java文档:thenApply(Functionfn)ReturnsanewCompletionStagethat,whenthisstagecompletesnormally,isexecutedwiththisstage'sresultastheargumenttothesuppliedfunction.thenCompose(Function>fn)ReturnsanewCompletionStagethat,whenthisstagecompletesnormally,is