Kotlin coroutine CoroutineScope asynchronous async cancel cancel await task

Kotlin coroutine CoroutineScope asynchronous async cancel cancel await task

 

import kotlinx.coroutines.*

fun main(args: Array<String>) {
    runBlocking {
        val mScope = CoroutineScope(Dispatchers.IO).async {
            println("->")
            delay(999999)
            println("<-")

            "done" //async返回的结果,可以在 mScope.await() 收到。
        }

        CoroutineScope(Dispatchers.IO).launch {
            println("await...")
            val result = mScope.await()
            println(result)
        }

        delay(2000)

        println("cancel...")
        mScope.cancel()
        println("cancel!")
    }

    println("end")
}

 

9e9c172303984bcdbaeec4fef00fd24e.png

 

 

 

 

Kotlin coroutine async and await_zhangphil's blog - CSDN blog runBlocking The three coroutines started internally 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. https://blog.csdn.net/zhangphil/article/details/129268399

kotlin coroutine coroutineScope_zhangphil's blog - CSDN blog coroutineScope creates an independent coroutine scope and does not end itself until all started coroutines are completed. runBlocking is very similar to coroutineScope. They both need to wait for all internal coroutines of the same scope to end before ending themselves. The main difference between the two is: runBlocking blocks the current thread, but coroutineScope does not. CoroutineScope will suspend and release the underlying thread for use by other coroutines. kotlin coroutine coroutineScope. https://blog.csdn.net/zhangphil/article/details/129265638 kotlin coroutine Job, CoroutineScope scope, Android_zhangphil’s blog-CSDN blog general programming techniques, for example, in Android, assume that it is implemented in the main thread A function, 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 managed function to do the dirty work in the managed function. After the processing is completed, Throw the result to the main thread. 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. https://blog.csdn.net/zhangphil/article/details/131096325 kotlin uses CoroutineScope to start the coroutine async and wait for the result to be returned_zhangphil's blog-CSDN blogGeneral 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. https://blog.csdn.net/zhangphil/article/details/129270875 Kotlin coroutine concurrency/parallel and serial switching, CoroutineScope and await_kotlin coroutine serial_zhangphil's blog-CSDN blog runBlocking 3 coroutines started internally The process performs time-consuming operations. From the output, you can see that 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. _kotlin coroutine serial https://blog.csdn.net/zhangphil/article/details/130794990

 

Guess you like

Origin blog.csdn.net/zhangphil/article/details/132621143