- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
scala.concurrent.Promise
我试图在 scaladoc 中找出 completeWith
和 tryCompleteWith
之间的区别,但没有太多解释,也没有示例。另外,我担心以我目前的专业水平阅读源代码可能会误导我。需要您提供一些示例的建议。
最佳答案
Promise.completeWith
只是一个调用 Promise.tryCompleteWith
的包装器:
/** Completes this promise with the specified future, once that future is completed.
*
* @return This promise
*/
final def completeWith(other: Future[T]): this.type = tryCompleteWith(other)
/** Attempts to complete this promise with the specified future, once that future is completed.
*
* @return This promise
*/
final def tryCompleteWith(other: Future[T]): this.type = {
other onComplete { this tryComplete _ }
this
}
老实说,我不知道他们为什么做出这个决定,因为它看起来只是一个简单的包装器,仅此而已。
正如@Alexey Romanov 指出的那样,this GitHub issue points out the reason for this弃用:
Calling
completeWith
when theDefaultPromise
is already completed, leads to callbacks not being properly executed.This happened because
Future.InternalCallbackExecutor
extendsBatchingExecutor
which assumesunbatchedExecute
to be async, when in this case it is sync, and if there is an exception thrown by executing the batch, it creates a new batch with the remaining items from the current batch and submits that tounbatchedExecute
and then rethrows, but if you have a syncunbatchedExecute
, it will fail since it is not reentrant, as witnessed by the failedrequire
as reported in this issue.This commit avoids problem by delegating
completeWith
totryComplete
, which has the effect of usingonComplete
+tryComplete
i.s.o.complete
, which means that when it fails (because of a benign race condition between completers) it won't throw an exception.
关于scala - Promise.completeWith 与 Promise.tryCompleteWith 有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44049906/
scala.concurrent.Promise 我试图在 scaladoc 中找出 completeWith 和 tryCompleteWith 之间的区别,但没有太多解释,也没有示例。另外,我担心
我是一名优秀的程序员,十分优秀!