我有以下使用协程的 Kotlin 代码片段,我对输出感到困惑。有人可以解释为什么在这两种情况下输出都是“A B”吗?
// Code Snippet 1
fun main() {
CoroutineScope(Dispatchers.IO).launch {
print("A")
}
print("B")
}
Run Code Online (Sandbox Code Playgroud)
输出:AB
// Code Snippet 2
fun main() {
CoroutineScope(Dispatchers.IO).launch {
delay(200)
print("A")
}
print("B")
}
Run Code Online (Sandbox Code Playgroud)
输出AB
我已经在 kotlin 游乐场上运行了这段代码(链接 - kotlin 游乐场 我无法理解为什么 A 在 B 之前打印,即使添加了 200ms 的延迟