调度器有多种,看下面的例子:
fun main() = runBlocking<Unit> {
launch { // context of the parent, main runBlocking coroutine
println("main runBlocking : I'm working in thread ${Thread.currentThread().name}")
}
launch(Dispatchers.Unconfined) { // not confined -- will work with main thread
println("Unconfined : I'm working in thread ${Thread.currentThread().name}")
}
launch(Dispatchers.Default) { // will get dispatched to DefaultDispatcher
println("Default : I'm working in thread ${Thread.currentThread().name}")
}
launch(newSingleThreadContext("MyOwnThread")) { // will get its own new thread
println("newSingleThreadContext: I'm working in thread ${Thread.currentThread().name}")
}
}
输出结果如下:
Unconfined : I'm working in thread main
Default : I'm working in thread DefaultDispatcher-worker-1
newSingleThreadContext: I'm working in thread MyOwnThread
main runBlocking : I'm working in thread main
当 launch 函数调用的时候,它会从 CoroutineScope 中继承 context,在上面的例子中,它继承的是 runBlocking中的 context,所以运行在主线程。
Dispatchers.Default 使用一个后台的线程池来运行。
newSingleThreadContext 每次会创建一个单独的线程来运行,最好是保存起来,复用同一个。
Dispatchers.Unconfined 这个就比较神奇,它会让线程运行在调用者线程,但是只到第一个挂起点。
看一个例子:
fun main() = runBlocking<Unit> {
launch(Dispatchers.Unconfined) { // not confined -- will work with main thread
println("Unconfined : I'm working in thread ${Thread.currentThread().name}")
delay(500)
println("Unconfined : After delay in thread ${Thread.currentThread().name}")
}
launch { // context of the parent, main runBlocking coroutine
println("main runBlocking: I'm working in thread ${Thread.currentThread().name}")
delay(1000)
println("main runBlocking: After delay in thread ${Thread.currentThread().name}")
}
}
输出结果:
Unconfined : I'm working in thread main
main runBlocking: I'm working in thread main
Unconfined : After delay in thread kotlinx.coroutines.DefaultExecutor
main runBlocking: After delay in thread main
发现了一个神奇的事情,第一个协程中,第二条打印语句跑到了子线程中去了,是被 delay 函数影响到了。
那为啥第二个协程不受影响呢?看一下第一个协程与第二个协程 context 的区别:
可以看到是因为第一个设置了 Dispatchers.Unconfined 之后覆盖了原本的 BlockingEventLoop 导致的。前面我们说过,delay 函数在 context 有调度器的情况下是不会添加一个 Default 的,这里也验证了。
子协程
当一个协程在另一个协程的 CoroutineScope 中启动时,它将通过 CoroutineScope.coroutineContext 继承其上下文,新协程的job将成为父协程 job 的子 job。当父协程被取消时,它的所有子协程也会被递归取消。
但是这种父子关系是可以被覆盖的,我们主要说一下 job。
fun main() = runBlocking<Unit> {
// launch a coroutine to process some kind of incoming request
val request = launch {
// it spawns two other jobs
launch(Job()) {
println("job1: I run in my own Job and execute independently!")
delay(1000)
println("job1: I am not affected by cancellation of the request")
}
// and the other inherits the parent context
launch {
delay(100)
println("job2: I am a child of the request coroutine")
delay(1000)
println("job2: I will not execute this line if my parent request is cancelled")
}
}
delay(500)
request.cancel() // cancel processing of the request
println("main: Who has survived request cancellation?")
delay(1000) // delay the main thread for a second to see what happens
}
request 协程有两个子协程,一个设置了 job,另一个没有。当取消 request 协程的时候,设置了 job 的协程不会取消。
输出如下:
job1: I run in my own Job and execute independently!
job2: I am a child of the request coroutine
main: Who has survived request cancellation?
job1: I am not affected by cancellation of the request
父协程的责任
父协程会等待所有子协程完成,嗯,这个不是字面上的意思,而是说,父协程的逻辑可以先走完,子协程依然会运行,比如:
fun main() = runBlocking<Unit> {
// launch a coroutine to process some kind of incoming request
val request = launch {
repeat(3) { i -> // launch a few children jobs
launch {
delay((i + 1) * 200L) // variable delay 200ms, 400ms, 600ms
println("Coroutine $i is done")
}
}
println("request: I'm done and I don't explicitly join my children that are still active")
}
request.join() // wait for completion of the request, including all its children
println("Now processing of the request is complete")
}
在这个程序里面,父协程会首先打印 I am done,子协程会后打印:
request: I'm done and I don't explicitly join my children that are still active
Coroutine 0 is done
Coroutine 1 is done
Coroutine 2 is done
Now processing of the request is complete
即使,不使用 join 方法,也是如此。