在他关于 Kotlin 中的结构化并发的文章 ( https://medium.com/@elizarov/structured-concurrency-722d765aa952 ) 中,Roman Elizarov 通过给出以下示例来解释并行分解:
coroutineScope {
val deferred1 = async { loadImage(name1) }
val deferred2 = async { loadImage(name2) }
combineImages(deferred1.await(), deferred2.await())
}
Run Code Online (Sandbox Code Playgroud)
显然,这段代码是不言自明的。但是,如果我们改为这样写,我们会得到相同的结果吗
coroutineScope {
val result1 = async { loadImage(name1) }.await()
val result2 = async { loadImage(name2) }.await()
combineImages(result1, result2)
}
Run Code Online (Sandbox Code Playgroud)
意思是,两个异步仍然并行运行还是第二个异步调用永远不会运行,直到 result1 可用?