为什么CompletableFuture和ListenableFuture的协程构建器之间存在差异?

问题描述 投票:4回答:1

在检查Kotlin协同程序的来源时,我注意到**之间存在差异(用JDK 8 CompletableFuture标记)

public fun <T> future(
    context: CoroutineContext = DefaultDispatcher,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    block: suspend CoroutineScope.() -> T
): CompletableFuture<T> {
    require(!start.isLazy) { "$start start is not supported" }
    val newContext = newCoroutineContext(context)
    val job = Job(newContext[Job])
    val future = CompletableFutureCoroutine<T>(newContext + job)
    job.cancelFutureOnCompletion(future)
    ** future.whenComplete { _, exception -> job.cancel(exception) } **
    start(block, receiver=future, completion=future) // use the specified start strategy
    return future
}


private class CompletableFutureCoroutine<T>(
    override val context: CoroutineContext
) : CompletableFuture<T>(), Continuation<T>, CoroutineScope {
    override val coroutineContext: CoroutineContext get() = context
    override val isActive: Boolean get() = context[Job]!!.isActive
    override fun resume(value: T) { complete(value) }
    override fun resumeWithException(exception: Throwable) { completeExceptionally(exception) }
    ** doesn't override cancel which corresponds to interrupt task **
}

Guava ListenableFuture

public fun <T> future(
    context: CoroutineContext = DefaultDispatcher,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    block: suspend CoroutineScope.() -> T
): ListenableFuture<T> {
    require(!start.isLazy) { "$start start is not supported" }
    val newContext = newCoroutineContext(context)
    val job = Job(newContext[Job])
    val future = ListenableFutureCoroutine<T>(newContext + job)
    job.cancelFutureOnCompletion(future)
    start(block, receiver=future, completion=future) // use the specified start strategy
    return future
}

private class ListenableFutureCoroutine<T>(
    override val context: CoroutineContext
) : AbstractFuture<T>(), Continuation<T>, CoroutineScope {
    override val coroutineContext: CoroutineContext get() = context
    override val isActive: Boolean get() = context[Job]!!.isActive
    override fun resume(value: T) { set(value) }
    override fun resumeWithException(exception: Throwable) { setException(exception) }
    ** override fun interruptTask() { context[Job]!!.cancel() } **
}

集成,虽然我认为类型几乎相同(当然除了ListenableFuture不能直接完成,但我不明白为什么这在这里很重要)。这种差异背后有一个具体原因吗?

kotlin guava java.util.concurrent coroutine kotlin-coroutines
1个回答
8
投票

CompletableFuture.cancel是一种开放(可覆盖)方法,但它不是为覆盖而设计的。它的文档没有为取消调用提供任何保证,所以唯一能够证明CompletableFuture被取消的未来证明(双关语)方法是在其上安装whenComplete监听器。

例如,对于未来版本的JDK添加另一种方法来取消未在内部调用cancel的未来,这是完全合法的。这样的改变不会违反任何一份CompletableFuture合同。

将此与AbstractFuture.interruptTask上的文档进行比较。此方法明确设计用于覆盖,其文档保证调用它的条件。因此,我们可以为ListenableFuture构建器提供稍微更高效的实现,避免创建lambda来在其上安装取消侦听器。

© www.soinside.com 2019 - 2024. All rights reserved.