小编San*_*ete的帖子

Scala异步/等待和并行化

我正在学习在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)

它们之间的唯一区别是使用中间变量.这怎么会影响并行化?

asynchronous scala future async-await

37
推荐指数
2
解决办法
1万
查看次数

标签 统计

async-await ×1

asynchronous ×1

future ×1

scala ×1