我正在学习在Scala中使用async/await.我在https://github.com/scala/async中看过这个
从理论上讲,这段代码是异步的(非阻塞),但它没有并行化:
def slowCalcFuture: Future[Int] = ...
def combined: Future[Int] = async {
await(slowCalcFuture) + await(slowCalcFuture)
}
val x: Int = Await.result(combined, 10.seconds)
Run Code Online (Sandbox Code Playgroud)
而另一个是并行化的:
def combined: Future[Int] = async {
val future1 = slowCalcFuture
val future2 = slowCalcFuture
await(future1) + await(future2)
}
Run Code Online (Sandbox Code Playgroud)
它们之间的唯一区别是使用中间变量.这怎么会影响并行化?