协程05 - 调度器

本文详细解释了Kotlin中的调度器(Dispatchers)如何影响协程执行,包括默认调度、独立线程、未限制调度以及协程间的父子关系。重点讨论了Dispatchers.Unconfined的作用以及为何第一个延迟后的协程会切换到子线程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

调度器有多种,看下面的例子:

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 方法,也是如此。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二手的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值