kotlin协程可以用同步方式写异步代码,自动实现对线程切换的管理,本文主要给大家讲解kotlin协程到底是怎么切换线程的,感兴趣的朋友跟随小编一起看看吧
随着kotlin在Android开发领域越来越火,协程在各个项目中的应用也逐渐变得广泛
但是协程到底是什么呢?
协程其实是个古老的概念,已经非常成熟了,但大家对它的概念一直存在各种疑问,众说纷纷
有人说协程是轻量级的线程,也有人说kotlin协程其实本质是一套线程切换方案
显然这对初学者不太友好,当不清楚一个东西是什么的时候,就很难进入为什么和怎么办的阶段了
本文主要就是回答这个问题,主要包括以下内容
1.关于协程的一些前置知识
2.协程到底是什么?
3.kotlin协程的一些基本概念,挂起函数,CPS转换,状态机等
以上问题总结为思维导图如下:
1. 前置知识
1.1 CoroutineScope
到底是什么?
CoroutineScope
即协程运行的作用域,它的源码很简单
public interface CoroutineScope { public val coroutineContext: CoroutineContext }
可以看出CoroutineScope
的代码很简单,主要作用是提供CoroutineContext
,协程运行的上下文
我们常见的实现有GlobalScope
,LifecycleScope
,ViewModelScope
等
1.2 GlobalScope
与ViewModelScope
有什么区别?
public object GlobalScope : CoroutineScope { /** * 返回 [EmptyCoroutineContext]. */ override val coroutineContext: CoroutineContext get() = EmptyCoroutineContext } public val ViewModel.viewModelScope: CoroutineScope get() { val scope: CoroutineScope? = this.getTag(JOB_KEY) if (scope != null) { return scope } return setTagIfAbsent( JOB_KEY, CloseableCoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) ) }
两者的代码都挺简单,从上面可以看出
1.GlobalScope
返回的为CoroutineContext
的空实现
2.ViewModelScope
则往CoroutineContext
中添加了Job
与Dispatcher
我们先来看一段简单的代码
fun testOne(){ GlobalScope.launch { print("1:" + Thread.currentThread().name) delay(1000) print("2:" + Thread.currentThread().name) } } //打印结果为:DefaultDispatcher-worker-1 fun testTwo(){ viewModelScope.launch { print("1:" + Thread.currentThread().name) delay(1000) print("2:" + Thread.currentThread().name) } } //打印结果为: main
上面两种Scope
启动协程后,打印当前线程名是不同的,一个是线程池中的一个线程,一个则是主线程
这是因为ViewModelScope
在CoroutineContext
中添加了Dispatchers.Main.immediate
的原因
我们可以得出结论:协程就是通过Dispatchers
调度器来控制线程切换的
1.3 什么是调度器?
从使用上来讲,调度器就是我们使用的Dispatchers.Main
,Dispatchers.Default
,Dispatcher.IO
等
从作用上来讲,调度器的作用是控制协程运行的线程
从结构上来讲,Dispatchers
的父类是ContinuationInterceptor
,然后再继承于CoroutineContext
它们的类结构关系如下:
这也是为什么Dispatchers
能加入到CoroutineContext
中的原因,并且支持+
操作符来完成增加
1.4 什么是拦截器
从命名上很容易看出,ContinuationInterceptor
即协程拦截器,先看一下接口
interface ContinuationInterceptor : CoroutineContext.Element { // ContinuationInterceptor 在 CoroutineContext 中的 Key companion object Key : CoroutineContext.Key/** * 拦截 continuation */ fun interceptContinuation(continuation: Continuation ): Continuation //... }
从上面可以提炼出两个信息
1.拦截器的Key
是单例的,因此当你添加多个拦截器时,生效的只会有一个
2.我们都知道,Continuation
在调用其Continuation#resumeWith()
方法,会执行其suspend
修饰的函数的代码块,如果我们提前拦截到,是不是可以做点其他事情?这就是调度器切换线程的原理
上面我们已经介绍了是通过Dispatchers
指定协程运行的线程,通过interceptContinuation
在协程恢复前进行拦截,从而切换线程
带着这些前置知识,我们一起来看下协程启动的具体流程,明确下协程切换线程源码具体实现
2. 协程线程切换源码分析
2.1 launch
方法解析
我们首先看一下协程是怎样启动的,传入了什么参数
public fun CoroutineScope.launch( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit ): Job { val newContext = newCoroutineContext(context) val coroutine = if (start.isLazy) LazyStandaloneCoroutine(newContext, block) else StandaloneCoroutine(newContext, active = true) coroutine.start(start, coroutine, block) return coroutine }
总共有3个参数:
1.传入的协程上下文
2.CoroutinStart
启动器,是个枚举类,定义了不同的启动方法,默认是CoroutineStart.DEFAULT
3.block
就是我们传入的协程体,真正要执行的代码
这段代码主要做了两件事:
1.组合新的CoroutineContext
2.再创建一个 Continuation
2.1.1 组合新的CoroutineContext
public actual fun CoroutineScope.newCoroutineContext(context: CoroutineContext): CoroutineContext { val combined = coroutineContext + context val debug = if (DEBUG) combined + CoroutineId(COROUTINE_ID.incrementAndGet()) else combined return if (combined !== Dispatchers.Default && combined[ContinuationInterceptor] == null) debug + Dispatchers.Default else debug }
从上面可以提炼出以下信息:
1.会将launch
方法传入的context
与<
以上就是Kotlin协程到底是如何切换线程的的详细内容,更多请关注0133技术站其它相关文章!