[英]Can i use thenCombine/thenCompose on a CompletableFuture more than once?
[英]If I use thenCompose on a CompletableFuture and assign it do I have 2 CompletableFutures?
假設我遇到以下情況:
CompletableFuture<Object1> f1 = Class1.doSomething();
CompletableFuture<Object2> f2 = Class2.doSomethingElse(f1);
boolean b = doAnotherThing(f2);
其中Class2使用thenCompose()並以不同的對象類型返回
CompletableFuture<Object2> doSomethingElse(CompletableFuture<Object1> f) {
return f.thenCompose(s -> {...});
那我有2個期貨嗎? 我需要做:
CompletableFuture.allOf(f1, f2);
任務如何影響未來?
您有兩個截然不同的可完成期貨。 f1
將使用doSomething
的Object1
結果完成。 f2
將使用doSomethingElse
匿名函數的Object2
結果完成。 但是,由於f2
取決於f1
的結果,因此您只需等待f2
的完成即可知道兩個任務均已完成。
根據java docs,thenCompose:
從接口:CompletionStage復制的描述返回一個新的CompletionStage,當此階段正常完成時,將使用此階段作為所提供函數的參數來執行該階段。 有關涵蓋異常完成的規則,請參閱CompletionStage文檔。
它返回一個新的CompletableFuture
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.