我正在阅读SIP-14,它的概念Future非常有意义且易于理解.但有两个问题Promise:
SIP说Depending on the implementation, it may be the case that p.future == p.怎么会这样?是Future和Promise不是两种不同的类型?
我们Promise什么时候应该使用?示例producer and consumer代码:
import scala.concurrent.{ future, promise }
val p = promise[T]
val f = p.future
val producer = future {
val r = produceSomething()
p success r
continueDoingSomethingUnrelated()
}
val consumer = future {
startDoingSomething()
f onSuccess {
case r => doSomethingWithResult()
}
}
Run Code Online (Sandbox Code Playgroud)很容易阅读,但我们真的需要这样写吗?我试图只用Future和没有Promise来实现它:
val f = future …Run Code Online (Sandbox Code Playgroud)