kotlin coroutine val vs fun

t.h*_*ill 3 coroutine kotlin

我是coroutine和Kotlin的新学徒.为什么我会得到不同的结果,下面的情况1和2?

fun main(args: Array<String>) = runBlocking {
    fun a() = async(CommonPool) {
        println("start A")
        delay(1000)
        println("finish A")
    }

    fun b() = async(CommonPool) {
        println("start B")
        delay(1000)
        println("finish B")
    }

    //case 1
    a().await()
    b().await()

    //case 2
    val A = a()
    val B = b()
    A.await()
    B.await()
}
Run Code Online (Sandbox Code Playgroud)

这种val样式编码是基本的吗?

hot*_*key 11

壳体1是等效于

val A = a()
await(A)
val B = b()
await(B)
Run Code Online (Sandbox Code Playgroud)

也就是说,你开始A,等待它(这里协程暂停),然后你才开始B,因此A并按B顺序执行,而不是同时执行.

案例2中,你启动两个A,B然后只有协同程序暂停等待AB.