为什么CompletableFuture方法不返回CompletionState

时间:2019-02-26 09:00:53

标签: java completable-future

handle上任何whenCompleteCompletableFuture方法的返回类型还是CompletableFuture而不是CompletionStage

但是,如果您要在所有返回的实例上完成CF,则不会执行handle的代码。

F.e。:

 val handled = AtomicBoolean(false)
 val cf = CompletableFuture<Unit>().whenComplete { _, _ -> handled.set(true) }
 cf.complete(Unit)
 handled.get().shouldBeTrue()

在上面的代码中,我希望将执行该句柄。但是事实并非如此。您可以完成原始的CF,然后再运行:

val handled = AtomicBoolean(false)
val cf = CompletableFuture<Unit>()
cf.whenComplete { _, _ -> handled.set(true) }
cf.complete(Unit)
handled.get().shouldBeTrue()

换句话说,在调用handle时返回CF非常混乱,您不应该完成它,而应该完成原始CF。

那么他们为什么不返回CompletionStage呢?那会禁止像我这样的人犯这样的错误?

0 个答案:

没有答案
相关问题