kotlin coroutine CoroutineScope Dispatchers.IO launch thread Id

kotlin coroutine CoroutineScope Dispatchers.IO launch thread Id

 

import kotlinx.coroutines.*

fun main(args: Array<String>) {
    println("main 线程id:${Thread.currentThread().threadId()}")

    CoroutineScope(Dispatchers.IO).launch {
        println("launch 线程id:${Thread.currentThread().threadId()}")

        foo()
    }

    //如果主线程不sleep,将看不到foo()的输出,因为main函数很快就执行结束导致整个程序退出,
    //main已退出,进程销毁,所以kotlin协程也被销毁
    Thread.sleep(100)
}

fun foo() {
    println("foo 线程id:${Thread.currentThread().threadId()}")
}

 

 

 

 

Kotlin coroutine concurrent/parallel and serial switching, CoroutineScope and await_zhangphil's blog - CSDN blog runBlocking 3 coroutines started internally do time-consuming operations. From the output, you can see that 3 coroutines are executed cross-concurrently, and runBlocking will wait until 3 Each coroutine exits after completing its execution, and 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/130794990

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 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

 

Guess you like

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