是一个接口,类似于一个通用回调。子接口 CancellableContinuation 表示可以取消的延续体。他们的实现类 ContinuationImpl 和 CancellableContinuationImpl 都是 intel 修饰的,意味着延续体对象不是通过自行创建获得,挂起函数通过suspendCoroutineUninterceptedOrReturn() 获得。
Continuation
| resumeWith( ) | public fun resumeWith(result: Result 携带结果恢复被挂起的协程,结果被封装在 Result 对象中,可以是: Result .success(传值) Result .failure(传异常) 为了方便调用可以使用以下两个扩展函数,不然需要手动将值或异常包装为Result对象使用 |
| Continuation.resume( ) Continuation.resumeWithException( ) | public inline fun 携带值 |
| public inline fun 携带异常 |
CancellableContinuation
| cancel( ) | public fun cancel(cause: Throwable? = null): Boolean 使用可选的异常来结束掉这个延续体,成功返回true。 |
| invokeOnCancellation( ) | public fun invokeOnCancellation(handler: CompletionHandler) 当延续体被取消或抛出异常时调用,一般用来释放资源。 |
| isActive isCompleted isCancelled | public val isActive: Boolean public val isCompleted: Boolean public val isCancelled: Boolean
|
其它相关类
| BaseContinuationImpl | 基本实现类,实现了resumeWith()以控制延续体状态机的执行流程,定义了invokeSuspend()抽象函数由Kotlin编译器自动实现,函数体的实现是协程中的执行代码块内容。 |
| ContinuationImpl | 继承自BaseContinuationImpl,增加了intercepted()拦截器功能,实现线程调度等。 |
| SuspendLambda | 继承自ContinuationImpl,是对协程代码块中代码的封装。 |
| 编译生成的匿名子类 | Kotlin在编译时,每个协程都会生成SuspendLambda的匿名子类并创建其对象(协程嵌套就有多个),有两层含义: ①对协程代码块中代码的封装,以挂起函数为分割点将代码分为多个部分填充到invokeSuspend()函数中从而实现SuspendLambda。 ②实现Continuation,同时又是当前协程的延续体,具有恢复协程执行的能力。 |
三层包装都实现了 Contineation 接口,通过代理模式(后者持有前者)将各层组合在一起。
| 1层 | CoroutineBuilder 协程构建器 | launch() → Job → StandaloneCoroutine → AbstractCoroutine → Coroutine async() → Deferred → DeferredCoroutine → AbstractCoroutine → Coroutine 返回的Job和Deferred封装了协程的状态并提供了取消协程的接口。 |
| 2层 | Suspend Funtion 挂起函数 | SuspendLambda → ContinuationImpl → BaseContinuationImpl → Coroutine 封装协程运算逻辑(invokeSuspend()函数),持有第一层包装(成员属性completion)。 |
| 3层 | CoroutineDispatcher 协程调度器 | DispatchedContinuation → Coroutine 封装线程调度逻辑(成员属性dispatcher),持有第二层包装(成员属性continuation)。 |

通过协程构建器或者其它地方启动一个协程都将创建一个协程对象,这个对象就是 AbstractCoroutine,该类继承了 Job、Contunation、CoroutineScope,意味着一个协程对象在被需要的地方灵活可以转为这三类对象。
| 【的子类】 | 【说明】 |
| BlockingCoroutine | runBlocking() 启动的协程对象 |
| LazyStandaloneCoroutine | launch() 启动的延迟执行的协程 |
| StandaloneCoroutine | launch() 启动的立即执行的协程 |
| LazyDeferredCoroutine | async() 启动的延迟执行的协程 |
| DeferredCoroutine | async() 启动的立即执行的协程 |
| DispatchedCoroutine | withContext() 启动的协程 |
| FlowCoroutine | flow() 相关的协程 |
挂起函数内部实现不是使用普通回调的形式,而是使用状态机 CPS(Continuation Passing Style)来处理不同的挂起点。挂起函数将执行过程分为多个 Continuation 片段,利用状态机的方式保证各个片段是顺序执行的(后面的代码只能在挂起函数执行完后才能执行)。