как сказано в документе ...
По умолчанию это последовательно
suspend fun taskOne(): Int {
delay(1000L)
return 5
}
suspend fun taskTwo(): Int {
delay(1000L)
return 5
}
val time = measureTimeMillis {
val one = taskOne()
val two = taskTwo()
println("The total is ${one + two}")
}
println("time is $time ms")
//output
//The total is 10
//The total 2006 ms
Вы можете сделать это одновременно, используя async
suspend fun taskOne(): Int {
delay(1000L)
return 5
}
suspend fun taskTwo(): Int {
delay(1000L)
return 5
}
val time = measureTimeMillis {
val one = async { taskOne() }
val two = async { taskTwo() }
println("The total is ${one.await() + two.await()}")
}
println("time is $time ms")
//output
//The total is 10
//The total 1016 ms