runBlocking

runBlocking 阻塞協程

以下程式碼,二個suspend函式逐一執行,doOne()執行完,才輪到doTwo執行()。
所以共執行約2秒鐘左右。
runBlocking會等待子協程(suspend 函式),執行完畢。

1
2
3
4
5
6
7
8
9
  fun coroutin05() = runBlocking {
    val startTime = System.currentTimeMillis()
    runBlocking {
        val one = doOne()
        val two = doTwo()
    }
    val duration = System.currentTimeMillis() - startTime
    println(" in ${duration} ms")
  }
 in 2009 ms

results matching ""

    No results matching ""