kotlin协程CoroutineScope Dispatchers.IO launch 线程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()}")
- }