CoroutineScope launch waits for await asynchronous coroutine async result to return, Kotlin
import kotlinx.coroutines.*
fun main(args: Array<String>) {
runBlocking {
println("async ...")
val task = async(context = Dispatchers.IO, start = CoroutineStart.LAZY) {
println("async delay... ${System.currentTimeMillis()}")
delay(3000)
println("async delay end ${System.currentTimeMillis()}")
"zhangphil"
}
println("async end")
println("launch ...")
CoroutineScope(Dispatchers.IO).launch {
println("launch await ...")
val r = task.await()
println("launch await : $r ${System.currentTimeMillis()}")
}
println("launch end")
}
}
async ...
async end
launch ...
launch end
launch await ...
async delay... 1703750881366
async delay end 1703750884380
launch await : zhangphil 1703750884381
Kotlin uses CoroutineScope to start the coroutine async and wait for the result to return_kotlin coroutine waits for the result of another thread - CSDN blog article has been viewed 1k times. General programming techniques, for example, in Android, assuming that a function is implemented in the main thread, but this function is a time-consuming operation, there is no doubt that the implementation of this function needs to be cut into a non-main thread for operation, then you can design a A managed function that does the dirty work in the managed function and throws the results to the main thread after the processing is completed. Result 1-a: 5 - tid:22. General programming techniques, for example, in Android, assuming that a function is implemented in the main thread, but this function is a time-consuming operation, there is no doubt that the implementation of this function needs to be cut into a non-main thread for operation, then you can design a A managed function that does the dirty work in the managed function and throws the results to the main thread after the processing is completed. _kotlin coroutine waits for the result of another thread https://blog.csdn.net/zhangphil/article/details/129270875 kotlin coroutine async and await_Android coroutine await-CSDN blog article has been viewed 574 times. The three coroutines started internally by runBlocking perform time-consuming operations. From the output, you can see that the three coroutines are executed cross-concurrently. runBlocking will wait until the execution of the three coroutines is completed before exiting. The output results are in a clear order. General programming techniques, for example, in Android, assuming that a function is implemented in the main thread, but this function is a time-consuming operation, there is no doubt that the implementation of this function needs to be cut into a non-main thread for operation, then you can design a A managed function that does the dirty work in the managed function and throws the results to the main thread after the processing is completed. Result 1-a: 5 - tid:22. Result 1-b: 5 - tid:24. Result 2-a: 9 - tid: 22. _Android coroutine await https://blog.csdn.net/zhangphil/article/details/129268399