模式 | 功能 |
---|---|
DEFAULT | 立即执行协程体 |
ATOMIC | 立即执行协程体,但在开始运行之前无法取消 |
UNDISPATCHED | 立即在当前线程执行协程体,直到第一个 suspend 调用 |
LAZY | 只有在需要的情况下运行 |
DEFAULT
默认启动一个新线程,类似于线程池
GlobalScope.launch() {
println("1 ${Thread.currentThread()}")
val job = GlobalScope.launch(start = CoroutineStart.DEFAULT) {
println("2 ${Thread.currentThread()}")
}
delay(100)
println("3 ${Thread.currentThread()}")
job.join()
println("4 ${Thread.currentThread()}")
}
Thread.sleep(1000)
}
运行结果
1 Thread[DefaultDispatcher-worker-1 @coroutine#1,5,main]
2 Thread[DefaultDispatcher-worker-2 @coroutine#2,5,main]
3 Thread[DefaultDispatcher-worker-1 @coroutine#1,5,main]
4 Thread[DefaultDispatcher-worker-1 @coroutine#1,5,main]