美文网首页
全局单线程协程Scope

全局单线程协程Scope

作者: CentForever | 来源:发表于2022-08-13 18:35 被阅读0次
newSingleThreadContext("Ctx1").use { ctx1 ->
    newSingleThreadContext("Ctx2").use { ctx2 ->
        runBlocking(ctx1) {
            log("Started in ctx1")
            withContext(ctx2) {
                log("Working in ctx2")
            }
            log("Back to ctx1")
        }
    }
}

launch(newSingleThreadContext("MyOwnThread")) { // will get its own new thread
    println("newSingleThreadContext: I'm working in thread ${Thread.currentThread().name}")
}

https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html#jumping-between-threads

导入协程

    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10'

全局协程测试

       coroutineIoScope.launch {
            Timber.e("coroutineIoScope thread id = " + Thread.currentThread().id)
        }
        coroutineDbScope.launch {
            Timber.e("coroutineDbScope thread id = " + Thread.currentThread().id)
        }
        coroutineFileScope.launch {
            Timber.e("coroutineFileScope thread id = " + Thread.currentThread().id)
        }
        coroutineMainScope.launch {
            Timber.e("coroutineMainScope thread id = " + Thread.currentThread().id)
        }
        Timber.e("Main thread id = " + Thread.currentThread().id)

协程文件

@file:OptIn(DelicateCoroutinesApi::class)

package com.mgg.core.coroutine

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.newSingleThreadContext

// https://www.jianshu.com/p/76d2f47b900d
val singleIOThreadContext = newSingleThreadContext("CoroutineIoScope")

val singleDBThreadContext = newSingleThreadContext("CoroutineDbScope")

val singleFileThreadContext = newSingleThreadContext("CoroutineFileScope")

val coroutineIoScope = CoroutineScope(singleIOThreadContext)

val coroutineDbScope = CoroutineScope(singleDBThreadContext)

val coroutineFileScope = CoroutineScope(singleFileThreadContext)

val coroutineMainScope = MainScope()

相关文章

  • 全局单线程协程Scope

    https://kotlinlang.org/docs/coroutine-context-and-dispatc...

  • 协程-基础3

    什么是协程作用域(Coroutine Scope)? 协程作用域是协程运行的作用范围,换句话说,如果这个作用域销毁...

  • 协程

    协程,又称微线程,纤程,协程是一种用户态的轻量级线程 协程是单线程 协程的好处: 1 没有上下文切换 ...

  • SRS流媒体服务器源码分析(一):Rtmp publish流程

    线程模型 srs使用了state-threads协程库,是单线程多协程模型。这个协程的概念类似于lua的协程,都是...

  • 协程原理:函数调用过程、参数和寄存器

    SRS是单进程、单线程、多协程结构,协程(coroutine)背景以后再介绍,这篇文章介绍协程的重要基础,理解了这...

  • 关于协程开发

    协程: 1.什么是协程 协程,经常被称为微线程,纤程,是一种多任务并发的操作手段定义:协程是运行在单线程中的并发程...

  • 协程

    协程 协程也叫纤程,微线程。协程的本质是一个单线程程序,所以协程不能够使用计算机多核资源。 能够高效的完成并发任务...

  • 协程的简单使用

    1、协程作用范围。 全局有效。 生命周期内有效。 1、基本使用。 开启同步协程。 输出: 123 开启异步协程。 ...

  • 协程

    协程:是单线程下的并发,又称微线程,纤程。英文名Coroutine。一句话说明什么是协程:协程是一种用户态的轻量级...

  • greenlet和gevent的简单使用

    greenlet模块内部使用了协程的概念,在单线程内,我们需手动调用switch切换协程,使用方法如下 执行结果 ...

网友评论

      本文标题:全局单线程协程Scope

      本文链接:https://www.haomeiwen.com/subject/wdiqgrtx.html