【问题标题】:Why is there a difference between coroutine builders for CompletableFuture and ListenableFuture?为什么 CompletableFuture 和 ListenableFuture 的协程构建器之间存在差异?
【发布时间】:2017-11-22 14:08:06
【问题描述】:

在检查 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】:

    CompletableFuture.cancel 是一种开放(可覆盖)方法,但它不是为覆盖而设计的。它的文档不为其取消调用提供任何保证,因此了解CompletableFuture 已取消的唯一面向未来(双关语)的方法是在其上安装whenComplete 侦听器。

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

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

    【讨论】:

      猜你喜欢
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      相关资源
      最近更新 更多