協程的掛起是個很重要也比較難懂的概念。
從協程的啓動開始講起。
@Test
fun 測試協程啓動() {
GlobalScope.launch(start = CoroutineStart.DEFAULT) 協程啓動的地方@
{
val 掛起方法的值 = 掛起方法("測試")
println(掛起方法的值)
}
}
suspend fun 掛起方法(參數: String): String {
kotlinx.coroutines.delay(200)
return 參數
}
下面看一下調用邏輯:
如果你按照這個路徑去找對應的方法實現,我相信你會提出幾個疑問:
1,(suspend R.() -> T)這個東東是什麼?
答:
想要更加深入瞭解它我們得看看字節碼了。(不懂字節碼的朋友可以點擊這兒)
public final 測試協程啓動()V
@Lorg/junit/Test;()
L0
LINENUMBER 16 L0
GETSTATIC kotlinx/coroutines/GlobalScope.INSTANCE : Lkotlinx/coroutines/GlobalScope;
CHECKCAST kotlinx/coroutines/CoroutineScope
ACONST_NULL
GETSTATIC kotlinx/coroutines/CoroutineStart.DEFAULT : Lkotlinx/coroutines/CoroutineStart;
L1
LINENUMBER 17 L1
NEW com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1
DUP
ALOAD 0
ACONST_NULL
INVOKESPECIAL com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1.<init> (Lcom/example/dugup/gbd/ExampleUnitTest;Lkotlin/coroutines/Continuation;)V
CHECKCAST kotlin/jvm/functions/Function2
ICONST_1
ACONST_NULL
L2
LINENUMBER 16 L2
INVOKESTATIC kotlinx/coroutines/BuildersKt.launch$default (Lkotlinx/coroutines/CoroutineScope;Lkotlin/coroutines/CoroutineContext;Lkotlinx/coroutines/CoroutineStart;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lkotlinx/coroutines/Job;
POP
L3
LINENUMBER 21 L3
RETURN
L4
LOCALVARIABLE this Lcom/example/dugup/gbd/ExampleUnitTest; L0 L4 0
MAXSTACK = 7
MAXLOCALS = 1
我們看看它做了什麼:
a,獲取了GlobalScope.INSTANCE,CoroutineStart.DEFAULT
b,創建com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1對象
c,調用kotlinx/coroutines/BuildersKt.launch$default啓動協程
tip:BuildersKt.launch$default可以在把BuildersKt反編譯成Java文件之後找到
// $FF: synthetic method
public static Job launch$default(CoroutineScope var0, CoroutineContext var1, CoroutineStart var2, Function2 var3, int var4, Object var5) {
return BuildersKt__Builders_commonKt.launch$default(var0, var1, var2, var3, var4, var5);
}
再看看com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1是個啥?
final class com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1 extends kotlin/coroutines/jvm/internal/SuspendLambda implements kotlin/jvm/functions/Function2 {
// access flags 0x2
private Lkotlinx/coroutines/CoroutineScope; p$
// access flags 0x11
public final invokeSuspend(Ljava/lang/Object;)Ljava/lang/Object;
L0
INVOKESTATIC kotlin/coroutines/intrinsics/IntrinsicsKt.getCOROUTINE_SUSPENDED ()Ljava/lang/Object;
L1
LINENUMBER 17 L1
ASTORE 5
ALOAD 0
GETFIELD com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1.label : I
TABLESWITCH
0: L2
1: L3
default: L4
L2
ALOAD 1
INVOKESTATIC kotlin/ResultKt.throwOnFailure (Ljava/lang/Object;)V
L5
ALOAD 0
GETFIELD com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1.p$ : Lkotlinx/coroutines/CoroutineScope;
ASTORE 2
L6
LINENUMBER 18 L6
ALOAD 0
GETFIELD com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1.this$0 : Lcom/example/dugup/gbd/ExampleUnitTest;
LDC "\u6d4b\u8bd5"
ALOAD 0
ALOAD 0
ALOAD 2
PUTFIELD com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1.L$0 : Ljava/lang/Object;
ALOAD 0
ICONST_1
PUTFIELD com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1.label : I
INVOKEVIRTUAL com/example/dugup/gbd/ExampleUnitTest.掛起方法 (Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
L7
DUP
ALOAD 5
IF_ACMPNE L8
L9
LINENUMBER 17 L9
ALOAD 5
ARETURN
L3
ALOAD 0
GETFIELD com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1.L$0 : Ljava/lang/Object;
CHECKCAST kotlinx/coroutines/CoroutineScope
ASTORE 2
L10
ALOAD 1
INVOKESTATIC kotlin/ResultKt.throwOnFailure (Ljava/lang/Object;)V
ALOAD 1
L8
CHECKCAST java/lang/String
ASTORE 3
L11
LINENUMBER 19 L11
L12
ICONST_0
ISTORE 4
L13
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
ALOAD 3
INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/Object;)V
L14
L15
LINENUMBER 20 L15
GETSTATIC kotlin/Unit.INSTANCE : Lkotlin/Unit;
ARETURN
L4
NEW java/lang/IllegalStateException
DUP
LDC "call to 'resume' before 'invoke' with coroutine"
INVOKESPECIAL java/lang/IllegalStateException.<init> (Ljava/lang/String;)V
ATHROW
L16
LOCALVARIABLE $this$協程啓動的地方 Lkotlinx/coroutines/CoroutineScope; L6 L4 2
LOCALVARIABLE 掛起方法的值 Ljava/lang/String; L11 L15 3
LOCALVARIABLE this Lcom/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1; L0 L16 0
LOCALVARIABLE $result Ljava/lang/Object; L0 L16 1
MAXSTACK = 5
MAXLOCALS = 6
// access flags 0x0
Ljava/lang/Object; L$0
// access flags 0x0
<init>(Lcom/example/dugup/gbd/ExampleUnitTest;Lkotlin/coroutines/Continuation;)V
ALOAD 0
ALOAD 1
PUTFIELD com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1.this$0 : Lcom/example/dugup/gbd/ExampleUnitTest;
ALOAD 0
ICONST_2
ALOAD 2
INVOKESPECIAL kotlin/coroutines/jvm/internal/SuspendLambda.<init> (ILkotlin/coroutines/Continuation;)V
RETURN
MAXSTACK = 3
MAXLOCALS = 3
// access flags 0x0
I label
// access flags 0x1010
final synthetic Lcom/example/dugup/gbd/ExampleUnitTest; this$0
public final create(Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Lkotlin/coroutines/Continuation;
L0
ALOAD 2
LDC "completion"
INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull (Ljava/lang/Object;Ljava/lang/String;)V
NEW com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1
DUP
ALOAD 0
GETFIELD com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1.this$0 : Lcom/example/dugup/gbd/ExampleUnitTest;
ALOAD 2
INVOKESPECIAL com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1.<init> (Lcom/example/dugup/gbd/ExampleUnitTest;Lkotlin/coroutines/Continuation;)V
ASTORE 3
ALOAD 1
CHECKCAST kotlinx/coroutines/CoroutineScope
ALOAD 3
ALOAD 1
CHECKCAST kotlinx/coroutines/CoroutineScope
PUTFIELD com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1.p$ : Lkotlinx/coroutines/CoroutineScope;
ALOAD 3
ARETURN
L1
LOCALVARIABLE this Lkotlin/coroutines/jvm/internal/BaseContinuationImpl; L0 L1 0
LOCALVARIABLE value Ljava/lang/Object; L0 L1 1
LOCALVARIABLE completion Lkotlin/coroutines/Continuation; L0 L1 2
MAXSTACK = 4
MAXLOCALS = 4
// access flags 0x11
public final invoke(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
ALOAD 0
ALOAD 1
ALOAD 2
CHECKCAST kotlin/coroutines/Continuation
INVOKEVIRTUAL com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1.create (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Lkotlin/coroutines/Continuation;
CHECKCAST com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1
GETSTATIC kotlin/Unit.INSTANCE : Lkotlin/Unit;
INVOKEVIRTUAL com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1.invokeSuspend (Ljava/lang/Object;)Ljava/lang/Object;
ARETURN
MAXSTACK = 3
MAXLOCALS = 3
}
com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1是個繼承SuspendLambda且實現了Function2的類
SuspendLambda
@SinceKotlin("1.3")
// Suspension lambdas inherit from this class
internal abstract class SuspendLambda(
public override val arity: Int,
completion: Continuation<Any?>?
) : ContinuationImpl(completion), FunctionBase<Any?>, SuspendFunction {
constructor(arity: Int) : this(arity, null)
public override fun toString(): String =
if (completion == null)
Reflection.renderLambdaToString(this) // this is lambda
else
super.toString() // this is continuation
}
Function2
/** A function that takes 2 arguments. */
public interface Function2<in P1, in P2, out R> : Function<R> {
/** Invokes the function with the specified arguments. */
public operator fun invoke(p1: P1, p2: P2): R
}
所以現在真相出來了,(suspend R.() -> T)最終生成了一個個繼承SuspendLambda且實現了Function2的類
2,BaseContinuationImpl.create( value: Any?, completion: Continuation<*>): Continuation<Unit>創建的是個啥?
因爲它的實現是:
public open fun create(value: Any?, completion: Continuation<*>): Continuation<Unit> {
throw UnsupportedOperationException("create(Any?;Continuation) has not been overridden")
}
這一部分的字節碼可以從 com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1找到:
public final create(Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Lkotlin/coroutines/Continuation;
L0
NEW com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1
DUP
ALOAD 0
GETFIELD com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1.this$0 : Lcom/example/dugup/gbd/ExampleUnitTest;
ALOAD 2
INVOKESPECIAL com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1.<init> (Lcom/example/dugup/gbd/ExampleUnitTest;Lkotlin/coroutines/Continuation;)V
ASTORE 3
ALOAD 1
CHECKCAST kotlinx/coroutines/CoroutineScope
ALOAD 3
ALOAD 1
CHECKCAST kotlinx/coroutines/CoroutineScope
PUTFIELD com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1.p$ : Lkotlinx/coroutines/CoroutineScope;
ALOAD 3
ARETURN
L1
LOCALVARIABLE this Lkotlin/coroutines/jvm/internal/BaseContinuationImpl; L0 L1 0
LOCALVARIABLE value Ljava/lang/Object; L0 L1 1
LOCALVARIABLE completion Lkotlin/coroutines/Continuation; L0 L1 2
MAXSTACK = 4
MAXLOCALS = 4
我們看看它做了什麼:
a,new了一個com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1對象
b,將傳入的Object類型參數value強轉成CoroutineScope並賦值給com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1.p$
c,返回com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1
所以答案也出來了BaseContinuationImpl.create( value: Any?, completion: Continuation<*>): Continuation<Unit>創建的是com/example/dugup/gbd/ExampleUnitTest$測試協程啓動$1
3,Continuation.resumeWith(result: Result<T>)的具體實現是啥?因爲你找到的是個接口方法。
/**
* Resumes the execution of the corresponding coroutine passing a successful or failed [result] as the
* return value of the last suspension point.
*/
public fun resumeWith(result: Result<T>)
它的具體實現可以在BaseContinuationImpl中找到:
public final override fun resumeWith(result: Result<Any?>) {
// This loop unrolls recursion in current.resumeWith(param) to make saner and shorter stack traces on resume
var current = this
var param = result
while (true) {
// Invoke "resume" debug probe on every resumed continuation, so that a debugging library infrastructure
// can precisely track what part of suspended callstack was already resumed
probeCoroutineResumed(current)
with(current) {
val completion = completion!! // fail fast when trying to resume continuation without completion
val outcome: Result<Any?> =
try {
val outcome = invokeSuspend(param)
if (outcome === COROUTINE_SUSPENDED) return
Result.success(outcome)
} catch (exception: Throwable) {
Result.failure(exception)
}
releaseIntercepted() // this state machine instance is terminating
if (completion is BaseContinuationImpl) {
// unrolling recursion via loop
current = completion
param = outcome
} else {
// top-level completion reached -- invoke and return
completion.resumeWith(outcome)
return
}
}
}
}
到這裏協程啓動的流程就看完了。
下面研究一下這個方法:
suspend fun 掛起方法(參數: String): String {
delay(1000)
return 參數
}
這是一個掛起函數,掛起是個什麼意思?可以理解爲“在沒有阻塞線程的情況下延遲協程一段時間,並在指定時間後恢復“。不阻塞的掛起,是個什麼意思?o((⊙﹏⊙))o
看看kotlin爲我們提供的一個官方的很明顯的會有掛起操作的delay方法。
public suspend fun delay(timeMillis: Long) {
if (timeMillis <= 0) return // don't delay
return suspendCancellableCoroutine sc@ { cont: CancellableContinuation<Unit> ->
cont.context.delay.scheduleResumeAfterDelay(timeMillis, cont)
}
}
cont.context.delay.scheduleResumeAfterDelay(timeMillis, cont)可以簡單類比爲Handler.postDelayed,也就是延遲多久執行某操作,scheduleResumeAfterDelay是延遲執行cont.resume方法。
suspendCancellableCoroutine方法,通過搜索我們還可以找到一個suspendCoroutine,區別是前一個可以取消。
public suspend inline fun <T> suspendCancellableCoroutine(
crossinline block: (CancellableContinuation<T>) -> Unit
): T =
suspendCoroutineUninterceptedOrReturn { uCont ->
val cancellable = CancellableContinuationImpl(uCont.intercepted(), resumeMode = MODE_CANCELLABLE)
// NOTE: Before version 1.1.0 the following invocation was inlined here, so invocation of this
// method indicates that the code was compiled by kotlinx.coroutines < 1.1.0
// cancellable.initCancellability()
block(cancellable)
cancellable.getResult()
}
這裏最明顯的方法就是suspendCoroutineUninterceptedOrReturn,來看看的它的實現:
@SinceKotlin("1.3")
@InlineOnly
@Suppress("UNUSED_PARAMETER", "RedundantSuspendModifier")
public suspend inline fun <T> suspendCoroutineUninterceptedOrReturn(crossinline block: (Continuation<T>) -> Any?): T =
throw NotImplementedError("Implementation of suspendCoroutineUninterceptedOrReturn is intrinsic")
o(* ̄︶ ̄*)o沒有沒實現,不要緊不要緊,因爲它有一大段註釋,它的主要功能就是獲取掛起函數中的當前延續實例,就是幫忙拿到Continuation實例。然後它還有一個Any?類型的返回值,如果返回的是COROUTINE_SUSPENDED這表示掛起函數暫停了執行並且不會返回任何結果,這種情況下,當結果可用於恢復計算時,通過在某個時刻調用Continuation.resumeWith來恢復執行。否則,返回值必須具有可賦值給[T]的類型,並表示此掛起函數的結果, 由於結果類型被聲明爲Any?,並且無法正確地進行類型檢查,但是其正確的返回類型仍然在暫停函數的簽名上。上面的話簡單點說就是掛起函數可以掛起也可以不掛起,返回COROUTINE_SUSPENDED就是表示掛起了,返回T類型的值就可以直接帶着結果走。那麼返回值如果獲取的呢?
internal fun getResult(): Any? {
installParentCancellationHandler()
if (trySuspend()) return COROUTINE_SUSPENDED
val state = this.state
if (state is CompletedExceptionally) throw recoverStackTrace(state.cause, this)
...//省略了可取消的邏輯
return getSuccessfulResult(state)
}
上面代碼中:
a,return COROUTINE_SUSPENDED ,掛起邏輯,表示目標協程還沒有執行完,需要等待執行結果
b,throw recoverStackTrace(state.cause, this),拿到異常結果
c,return getSuccessfulResult(state),拿到正常結果
再看看COROUTINE_SUSPENDED是個啥?
@SinceKotlin("1.3")
public val COROUTINE_SUSPENDED: Any get() = CoroutineSingletons.COROUTINE_SUSPENDED
@SinceKotlin("1.3")
@PublishedApi
internal enum class CoroutineSingletons { COROUTINE_SUSPENDED, UNDECIDED, RESUMED }
下面再看看一下代碼:
companion object {
@JvmStatic
suspend fun suspendFuncDelay() =
kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn<String> {
println("${Thread.currentThread().name}:1")
thread {
Thread.sleep(1000)
println("${Thread.currentThread().name}:2")
it.resume("${Thread.currentThread().name}:4")
}
println("${Thread.currentThread().name}:3")
kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED
}
@JvmStatic
suspend fun suspendFuncImmediately() =
kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn<String> {
println("${Thread.currentThread().name}:1")
"${Thread.currentThread().name}:immediately."
}
}
上面有2個掛起函數,第一個會掛起,第二個直接返回的。
@Test
fun ceshi() = runBlocking {
println(1)
println(suspendFuncDelay())
println(2)
delay(1000)
println(3)
println(suspendFuncImmediately())
println(4)
}
運行一下結果:
1
main @coroutine#1:1
main @coroutine#1:3
Thread-0:2
Thread-0:4
2
3
main @coroutine#1:1
main @coroutine#1:immediately.
4
下面用Java仿寫這段邏輯:
public class ContinuationImpl implements Continuation<Object> {
private int lable = 0;
private final Continuation<Unit> completion;
public ContinuationImpl(Continuation<Unit> completion) {
this.completion = completion;
}
@NotNull
@Override
public CoroutineContext getContext() {
return EmptyCoroutineContext.INSTANCE;
}
@Override
public void resumeWith(@NotNull Object o) {
try {
Object result = o;
switch (lable) {
case 0: {
System.out.println(1);
result = ExampleUnitTest.suspendFuncDelay(this);
lable++;
if (isSuspended(result)) return;
}
case 1:{
System.out.println(result);
System.out.println(2);
result = DelayKt.delay(1000,this);
lable++;
if (isSuspended(result)) return;
}
case 2:{
System.out.println(3);
result = ExampleUnitTest.suspendFuncImmediately(this);
lable++;
if (isSuspended(result)) return;
}
case 3:{
System.out.println(result);
System.out.println(4);
}
}
completion.resumeWith(Unit.INSTANCE);
} catch (Exception e) {
completion.resumeWith(e);
}
}
private boolean isSuspended(Object result) {
return result == IntrinsicsKt.getCOROUTINE_SUSPENDED();
}
}
ContinuationImpl這個類在kotlin的協程標準庫裏面找到,上面提到的SuspendLambda就是繼承與ContinuationImpl,標準庫中的resumeWith最終調用到的是invokeSuspend,invokeSuspend就是我們的協程體。
有了這個類我們還需要準備一個 completion 用來接收結果:
public class RunSuspend implements Continuation<Unit> {
private Object result;
@NotNull
@Override
public CoroutineContext getContext() {
return EmptyCoroutineContext.INSTANCE;
}
@Override
public void resumeWith(@NotNull Object o) {
synchronized (this) {
this.result = o;
notifyAll();
}
}
public void await() throws Throwable {
synchronized (this) {
while (true) {
Object result = this.result;
if (result == null) {
wait();
} else if (result instanceof Throwable) {
throw (Throwable) result;
} else {
return;
}
}
}
}
}
最終運行:
@Test
fun ceshiJava(){
val runSuspend = RunSuspend()
val table = ContinuationImpl(runSuspend)
table.resumeWith(Unit)
runSuspend.await()
}
結果:
1
main:1
main:3
Thread-0:2
Thread-0:4
2
3
kotlinx.coroutines.DefaultExecutor:1
kotlinx.coroutines.DefaultExecutor:immediately.
4
運行結果和上面是一樣的。
到這裏應該對協程的本質有了一定的瞭解:
協程的掛起函數本質上就是一個回調,回調類型就是 Continuation
協程體的執行就是一個狀態機,每一次遇到掛起函數,都是一次狀態轉移,就像我們前面例子中的 label 不斷的自增來實現狀態流轉一樣
附錄:
@Nullable
public final Object suspendFuncImmediately(@NotNull Continuation $completion) {
int var3 = false;
StringBuilder var10000 = new StringBuilder();
Thread var10001 = Thread.currentThread();
Intrinsics.checkExpressionValueIsNotNull(var10001, "Thread.currentThread()");
String var4 = var10000.append(var10001.getName()).append(":1").toString();
boolean var5 = false;
System.out.println(var4);
var10000 = new StringBuilder();
var10001 = Thread.currentThread();
Intrinsics.checkExpressionValueIsNotNull(var10001, "Thread.currentThread()");
String var6 = var10000.append(var10001.getName()).append(":immediately.").toString();
if (var6 == IntrinsicsKt.getCOROUTINE_SUSPENDED()) {
DebugProbesKt.probeCoroutineSuspended($completion);
}
return var6;
}
這個是把上面suspendFuncImmediately函數反編譯成Java之後的代碼,可以看到suspendFuncImmediately的參數多了一個Continuation,返回值變成了Obeject。
啓動協程的3個參數都已經講完了
CoroutineScope.launch(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> Unit
): Job
那麼下一張將CoroutineScope。