如果我在 CompletableFuture 上使用 thenCompose 并分配它,我是否有 2 个 CompletableFutures?

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() 和 returns 不同的对象类型

CompletableFuture<Object2> doSomethingElse(CompletableFuture<Object1> f) {
return f.thenCompose(s -> {...});

那我有2个期货吗?我需要做吗:

CompletableFuture.allOf(f1, f2);

这项任务对未来有何影响?

根据 java 文档,然后编写:

Description copied from interface: CompletionStage Returns a new CompletionStage that, when this stage completes normally, is executed with this stage as the argument to the supplied function. See the CompletionStage documentation for rules covering exceptional completion.

它returns一个新的CompletableFuture

http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html#thenCompose-java.util.function.Function-

您有两个不同的可完成期货。 f1 将以 doSomethingObject1 结果完成。 f2 将用 doSomethingElse 中匿名函数的 Object2 结果完成。但是,由于 f2 取决于 f1 的结果,您只需等待 f2 完成即可知道两个任务都已完成。