coroutinescope专题

3.2-CoroutineScope/CoroutineContext:GlobalScope

文章目录 GlobalScope 是一个特殊的 CoroutineScope,它是一个单例的 CoroutineScope,我们可以直接用它启动协程: GlobalScope.launch {} 我们在 IDE 用 GlobalScope 时会有一条黄线,提示的是要小心使用它因为容易用错、容易写出问题: 当然我们理解了它的作用和适用场景后,是可以放心的使用它的。 前面

3.1-CoroutineScope/CoroutineContext:CoroutineScope 和 CoroutineContext 的概念

文章目录 CoroutineContext 的概念CoroutineScope 的概念 在使用协程的时候我们会经常跟 CoroutineScope 和 CoroutineContext 打交道,但是对它们的之间有什么区别、概念是什么还是很模糊。在这里就把它们先讲清楚。 CoroutineContext 的概念 CoroutineContext 的概念其实顾名思义就是 协程的上下

Kotlin协程CoroutineScope命名空间CoroutineName,Kotlin

Kotlin协程CoroutineScope命名空间CoroutineName,Kotlin       import kotlinx.coroutines.*fun main(args: Array<String>) {val myName = CoroutineName("fly")runBlocking {CoroutineScope(Dispatchers.IO).launch

Kotlin runBlocking CoroutineScope synchronized简单死锁场景

Kotlin runBlocking CoroutineScope synchronized简单死锁场景 import kotlinx.coroutines.*fun main(args: Array<String>) {runBlocking {val lock1 = Any()val lock2 = Any()CoroutineScope(Dispatchers.IO).launch {

CoroutineScope Dispatchers.IO异步操作async返回函数,Kotlin

CoroutineScope Dispatchers.IO异步操作async返回函数,Kotlin       import kotlinx.coroutines.*fun myFun(a: Int, b: Int, retFun: (sum: Int) -> Unit) {println("分支myFun开始... ${System.currentTimeMillis()}")val t

【Kotlin 协程】协程底层实现 ③ ( 结构化并发 | MainScope 作用域 | 取消协程作用域 | Activity 实现 CoroutineScope 协程作用域接口 )

文章目录 一、MainScope 协程作用域二、取消 MainScope 协程作用域三、Activity 实现 CoroutineScope 协程作用域接口 常见的 CoroutineScope 协程作用域 : GlobalScope : 该作用域是 进程级别的 , 与应用进程同级 , 即使 Activity 被销毁 , 协程任务也可以继续执行 ;MainScope : 该