Java kotlin协同程序是如何在后台实现的?

Java kotlin协同程序是如何在后台实现的?,java,kotlin,kotlin-coroutines,Java,Kotlin,Kotlin Coroutines,Kotlin协程是该语言的关键特性之一。我很好奇他们是如何实施的。所以我写了这个kotlin代码片段 CoroutineScope(Dispatchers.IO).launch { someFun(6) // A suspend fun with single int argument. } // Which is converted into a synthetic method with an extra argument of continuation type added a

Kotlin协程是该语言的关键特性之一。我很好奇他们是如何实施的。所以我写了这个kotlin代码片段

CoroutineScope(Dispatchers.IO).launch {
    someFun(6)  // A suspend fun with single int argument.
}  // Which is converted into a synthetic method with an extra argument of continuation type added at the end.
然后将其编译成字节码,然后反编译成Java(在Android Studio中)。我知道了

BuildersKt.launch$default(CoroutineScopeKt.CoroutineScope((CoroutineContext)Dispatchers.getIO()), (CoroutineContext)null, (CoroutineStart)null, (Function2)(new Function2((Continuation)null) {
   private CoroutineScope p$;
   Object L$0;
   int label;
            
   @Nullable
   public final Object invokeSuspend(@NotNull Object $result) {
      Object var3 = IntrinsicsKt.getCOROUTINE_SUSPENDED();
      CoroutineScope $this$launch;
      switch(this.label) {
      case 0:
         ResultKt.throwOnFailure($result);
         $this$launch = this.p$;
         MainActivity var10000 = MainActivity.this;
         this.L$0 = $this$launch;
         this.label = 1;
         if (var10000.someFun(6, this) == var3) {
            return var3;
         }
         break;
      case 1:
         $this$launch = (CoroutineScope)this.L$0;
         ResultKt.throwOnFailure($result);
         break;
      default:
         throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
      }
            
      return Unit.INSTANCE;
   }
            
   @NotNull
   public final Continuation create(@Nullable Object value, @NotNull Continuation completion) {
      Intrinsics.checkNotNullParameter(completion, "completion");
      Function2 var3 = new <anonymous constructor>(completion);
      var3.p$ = (CoroutineScope)value;
      return var3;
   }
            
   public final Object invoke(Object var1, Object var2) {
      return ((<undefinedtype>)this.create(var1, (Continuation)var2)).invokeSuspend(Unit.INSTANCE);
   }
}), 3, (Object)null);
BuildersKt.launch$default(CoroutineScopeKt.CoroutineScope((CoroutineContext)Dispatchers.getIO()),(CoroutineContext)null,(CoroutineStart)null,(Function2)(新函数2((Continuation)null){
私人合作视野p$;
对象L$0;
int标签;
@可空
公共最终对象调用uspend(@NotNull Object$result){
Object var3=IntrinsicsKt.getCOROUTINE_SUSPENDED();
CoroutineScope$此$launch;
开关(此.标签){
案例0:
结果kt.throwOnFailure($result);
$this$launch=this.p$;
MainActivity var10000=MainActivity.this;
this.L$0=$this$launch;
这个标签=1;
if(var10000.someFun(6,this)=var3){
返回变量3;
}
打破
案例1:
$this$launch=(CoroutineScope)this.L$0;
结果kt.throwOnFailure($result);
打破
违约:
抛出新的IllegalStateException(“在使用协同例程“invoke”之前调用“resume”);
}
返回Unit.INSTANCE;
}
@NotNull
公共最终继续创建(@Nullable对象值,@NotNull继续完成){
Intrinsics.checkNotNullParameter(completion,“completion”);
Function2 var3=new answer,一个挂起函数在每个挂起点被分成多个连续体,然后通过调用
invokeSuspend
continuosly从一开始就开始执行。每次调用
invokeSuspend
时,它都会执行相应的开关情况,然后更新
标签调用
invokeSuspend
时,它将运行下一个案例。当获得挂起函数的调用时,它停止调用
invokeSuspend
并等待恢复调用。一旦获得恢复调用,它将再次正常继续。这就是我所知道的

在第一行,Function2构造函数正在接受一个空对象以继续。但是为什么
Function2
类有一个单参数构造函数呢?我不知道Function2的实现。我搜索了它的源代码,但没有得到它。如果可能的话,我想要一个到
kotlin.jvm.functions.Function2
源代码的链接

在案例0:,如果(var10000.someFun(6,this)=var3)返回var3;
实际上意味着什么

在上一次调用invokeSuspend完成后,谁负责调用invokeSuspend。它如何知道invokeSuspend的最后一个案例何时执行

3,(Object)null
launch$default

的最后一个参数中代表了什么