阅读 158

协程入门(三):调度器

常用调度器

调度器 描述
缺省的 等待被调度,运行在当前线程上
Default 等待被调度,运行新的线程上
Main 等待被调度,运行在UI线程上
IO 等待被调度,运行新的线程上,适合IO操作
Unconfined 立即被调度执行,运行在当前线程上

示例

缺省

/**
     * 缺省的调度器是空的,默认运行在当前线程上
     *
     * 打印出:
     *
     * runBlocking thread = Instr: android.support.test.runner.AndroidJUnitRunner,1397
     * runBlocking end
     * launch thread = Instr: android.support.test.runner.AndroidJUnitRunner,1397
     */
    @Test
    fun dispatcher() = runBlocking {
        println(" runBlocking thread = ${Thread.currentThread().name},${Thread.currentThread().id}")
        launch {
            println(" launch thread = ${Thread.currentThread().name},${Thread.currentThread().id}")
        }
        println(" runBlocking end")
        delay(3000)
    }
复制代码

default

    /**
     * 使用default调度器(注意和缺省的不一样),会另起新线程执行协程体内容
     *
     * 打印出:
     *
     * runBlocking thread = Instr: android.support.test.runner.AndroidJUnitRunner,1437
     * runBlocking end
     * default thread = DefaultDispatcher-worker-2,1440
     * runBlocking end2
     * default end
     */
    @Test
    fun dispatcherDefault() = runBlocking {
        println(" runBlocking thread = ${Thread.currentThread().name},${Thread.currentThread().id}")
        launch(Dispatchers.Default) {
            println(" default thread = ${Thread.currentThread().name},${Thread.currentThread().id}")
            //因为是新线程,所以sleep不影响runBlocking end2输出
            Thread.sleep(10000)
            println(" default end")
        }
        println(" runBlocking end")
        //不同线程,sleep不会导致launch也跟着等待
        Thread.sleep(3000)
        println(" runBlocking end2")
    }
复制代码

main

    /**
     *
     * main调度器会在主线程执行协程体内容
     *
     *
     * 输出:
     *
     * runBlocking thread = Instr: android.support.test.runner.AndroidJUnitRunner,1457
     * runBlocking end
     * main thread = main,1
     */
    @Test
    fun dispatcherMain() = runBlocking {
        println(" runBlocking thread = ${Thread.currentThread().name},${Thread.currentThread().id}")
        launch(Dispatchers.Main) {
            println(" main thread = ${Thread.currentThread().name},${Thread.currentThread().id}")
        }
        println(" runBlocking end")
    }
复制代码

IO

    /**
     * 使用io调度器,和default类似会另起新线程执行协程体内容,只是更适合于密集的io操作
     *
     * 打印出:
     *
     * runBlocking thread = Instr: android.support.test.runner.AndroidJUnitRunner,1477
     * runBlocking end
     * IO thread = DefaultDispatcher-worker-2,1480
     * runBlocking end2
     * IO end
     */
    @Test
    fun dispatcherIO() = runBlocking {
        println(" runBlocking thread = ${Thread.currentThread().name},${Thread.currentThread().id}")
        launch(Dispatchers.IO) {
            println(" IO thread = ${Thread.currentThread().name},${Thread.currentThread().id}")
            //因为是新线程,所以sleep不影响runBlocking end2输出
            Thread.sleep(10000)
            println(" IO end")
        }
        println(" runBlocking end")
        //不同线程,sleep不会导致launch也跟着等待
        Thread.sleep(3000)
        println(" runBlocking end2")
    }
复制代码

Unconfined

    /**
     * 使用Unconfined调度器,会立即在当前线程执行协程体内容,是目前调度器中唯一会堵塞当前线程的
     *
     * 打印出:
     *
     * runBlocking thread = Instr: android.support.test.runner.AndroidJUnitRunner,1482
     * Unconfined thread = Instr: android.support.test.runner.AndroidJUnitRunner,1482
     * Unconfined end
     * runBlocking end
     * runBlocking end2
     * IO end
     */
    @Test
    fun dispatcherUnconfined() = runBlocking {
        println(" runBlocking thread = ${Thread.currentThread().name},${Thread.currentThread().id}")
        launch(Dispatchers.Unconfined) {
            println(" Unconfined thread = ${Thread.currentThread().name},${Thread.currentThread().id}")
            Thread.sleep(10000)
            println(" Unconfined end")
        }
        //相同线程,需要等待Unconfined执行完毕才可以接着往下执行
        println(" runBlocking end")
        //
        Thread.sleep(3000)
        println(" runBlocking end2")
    }
复制代码

微信公众号

关注下面的标签,发现更多相似文章
评论