9.9 协程上下文与调度器
到这里,我们已经看到了下面这些启动协程的方式:
launch(CommonPool) {...}
async(CommonPool) {...}
run(NonCancellable) {...}
这里的CommonPool 和 NonCancellable 是协程上下文(coroutine contexts)。本小节我们简单介绍一下自定义协程上下文。
9.9.1 调度和线程
协程上下文包括一个协程调度程序, 它可以指定由哪个线程来执行协程。调度器可以将协程的执行调度到一个线程池,限制在特定的线程中;也可以不作任何限制,让它无约束地运行。请看下面的示例:
fun testDispatchersAndThreads() = runBlocking {
val jobs = arrayListOf<Job>()
jobs += launch(Unconfined) {
// 未作限制 -- 将会在 main thread 中执行
println("Unconfined: I'm working in thread ${Thread.currentThread()}")
}
jobs += launch(coroutineContext) {
// 父协程的上下文 : runBlocking coroutine
println("coroutineContext: I'm working in thread ${Thread.currentThread()}")
}
jobs += launch(CommonPool) {
// 调度指派给 ForkJoinPool.commonPool
println("CommonPool: I'm working in thread ${Thread.currentThread()}")
}
jobs += launch(newSingleThreadContext("MyOwnThread")) {
// 将会在这个协程自己的新线程中执行
println("newSingleThreadContext: I'm working in thread ${Thread.currentThread()}")
}
jobs.forEach { it.join() }
}
运行上面的代码,我们将得到以下输出 (可能按不同的顺序):
Unconfined: I'm working in thread Thread[main,5,main]
CommonPool: I'm working in thread Thread[ForkJoinPool.commonPool-worker-1,5,main]
newSingleThreadContext: I'm working in thread Thread[MyOwnThread,5,main]
context: I'm working in thread Thread[main,5,main]
从上面的结果,我们可以看出:
使用无限制的Unconfined上下文的协程运行在主线程中;
继承了 runBlocking {…} 的context的协程继续在主线程中执行;
而CommonPool在ForkJoinPool.commonPool中;
我们使用newSingleThreadContext函数新建的协程上下文,该协程运行在自己的新线程Thread[MyOwnThread,5,main]中。
另外,我们还可以在使用 runBlocking的时候显式指定上下文, 同时使用 run 函数来更改协程的上下文:
fun log(msg: String) = println("${Thread.currentThread()} $msg")
fun testRunBlockingWithSpecifiedContext() = runBlocking {
log("$context")
log("${context[Job]}")
log("开始")
val ctx1 = newSingleThreadContext("线程A")
val ctx2 = newSingleThreadContext("线程B")
runBlocking(ctx1) {
log("Started in Context1")
run(ctx2) {
log("Working in Context2")
}
log("Back to Context1")
}
log("结束")
}
运行输出:
Thread[main,5,main] [BlockingCoroutine{Active}@b1bc7ed, EventLoopImpl@7cd84586]
Thread[main,5,main] BlockingCoroutine{Active}@b1bc7ed
Thread[main,5,main] 开始
Thread[线程A,5,main] Started in Context1
Thread[线程B,5,main] Working in Context2
Thread[线程A,5,main] Back to Context1
Thread[main,5,main] 结束
9.9.2 父子协程
当我们使用协程A的上下文启动另一个协程B时, B将成为A的子协程。当父协程A任务被取消时, B以及它的所有子协程都会被递归地取消。代码示例如下:
fun testChildrenCoroutine()= runBlocking<Unit> {
val request = launch(CommonPool) {
log("ContextA1: ${context}")
val job1 = launch(CommonPool) {
println("job1: 独立的协程上下文!")
delay(1000)
println("job1: 不会受到request.cancel()的影响")
}
// 继承父上下文:request的context
val job2 = launch(context) {
log("ContextA2: ${context}")
println("job2: 是request coroutine的子协程")
delay(1000)
println("job2: 当request.cancel(),job2也会被取消")
}
job1.join()
job2.join()
}
delay(500)
request.cancel()
delay(1000)
println("main: Who has survived request cancellation?")
}
运行输出:
Thread[ForkJoinPool.commonPool-worker-1,5,main] ContextA1: [StandaloneCoroutine{Active}@5b646af2, CommonPool]
job1: 独立的协程上下文!
Thread[ForkJoinPool.commonPool-worker-3,5,main] ContextA2: [StandaloneCoroutine{Active}@75152aa4, CommonPool]
job2: 是request coroutine的子协程
job1: 不会受到request.cancel()的影响
main: Who has survived request cancellation?
当前内容版权归 JackChan1999 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 JackChan1999 .