Last active
November 25, 2017 06:48
-
-
Save petrucci34/2f0eb03bec8a33c6e395a12e2f1a47a9 to your computer and use it in GitHub Desktop.
Kotlin Coroutines Implementation Details
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The Kotlin code below is decompiled into the Java code in this file. | |
/* | |
fun meaninglessCounter(): Int { | |
var counter = 0 | |
for (i in 1..10_000_000_000) { | |
counter += 1 | |
} | |
return counter | |
} | |
fun main(args: Array<String>) { | |
// Concurrent execution. | |
var time2 = measureTimeMillis { | |
val one = async { meaninglessCounter() } | |
val two = async { meaninglessCounter() } | |
runBlocking { | |
println("The answer is ${one.await() + two.await()}") | |
} | |
} | |
println("Concurrent completed in $time2 ms\n") | |
} | |
*/ | |
// Test2Kt$main$time2$1$two$1.java | |
import kotlin.Metadata; | |
import kotlin.Unit; | |
import kotlin.coroutines.experimental.Continuation; | |
import kotlin.coroutines.experimental.intrinsics.IntrinsicsKt; | |
import kotlin.coroutines.experimental.jvm.internal.CoroutineImpl; | |
import kotlin.jvm.functions.Function2; | |
import kotlin.jvm.internal.Intrinsics; | |
import kotlinx.coroutines.experimental.CoroutineScope; | |
import org.jetbrains.annotations.NotNull; | |
import org.jetbrains.annotations.Nullable; | |
@Metadata( | |
mv = {1, 1, 5}, | |
bv = {1, 0, 1}, | |
k = 3, | |
d1 = {"\u0000\u000e\n\u0000\n\u0002\u0010\b\n\u0002\u0018\u0002\n\u0002\b\u0002\u0010\u0000\u001a\u00020\u0001*\u00020\u0002H\u008a@ø\u0001\u0000¢\u0006\u0004\b\u0003\u0010\u0004"}, | |
d2 = {"<anonymous>", "", "Lkotlinx/coroutines/experimental/CoroutineScope;", "invoke", "(Lkotlinx/coroutines/experimental/CoroutineScope;Lkotlin/coroutines/experimental/Continuation;)Ljava/lang/Object;"} | |
) | |
final class Test2Kt$main$time2$1$two$1 extends CoroutineImpl implements Function2 { | |
private CoroutineScope p$; | |
// $FF: synthetic method | |
// $FF: bridge method | |
public Object invoke(Object var1, Object var2) { | |
return this.invoke((CoroutineScope)var1, (Continuation)var2); | |
} | |
@Nullable | |
public final Object doResume(@Nullable Object var1, @Nullable Throwable var2) { | |
Object var4 = IntrinsicsKt.getCOROUTINE_SUSPENDED(); | |
switch(super.label) { | |
case 0: | |
if(var2 != null) { | |
throw var2; | |
} | |
CoroutineScope var3 = this.p$; | |
return Integer.valueOf(Test2Kt.meaninglessCounter()); | |
default: | |
throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine"); | |
} | |
} | |
Test2Kt$main$time2$1$two$1(Continuation var1) { | |
super(2, var1); | |
} | |
@NotNull | |
public final Continuation create(@NotNull CoroutineScope $receiver, @NotNull Continuation $continuation) { | |
Intrinsics.checkParameterIsNotNull($receiver, "$receiver"); | |
Intrinsics.checkParameterIsNotNull($continuation, "$continuation"); | |
Test2Kt$main$time2$1$two$1 var3 = new Test2Kt$main$time2$1$two$1($continuation); | |
var3.p$ = $receiver; | |
return var3; | |
} | |
@Nullable | |
public final Object invoke(@NotNull CoroutineScope $receiver, @NotNull Continuation $continuation) { | |
Intrinsics.checkParameterIsNotNull($receiver, "$receiver"); | |
Intrinsics.checkParameterIsNotNull($continuation, "$continuation"); | |
return ((Test2Kt$main$time2$1$two$1)this.create($receiver, $continuation)).doResume(Unit.INSTANCE, (Throwable)null); | |
} | |
// $FF: synthetic method | |
// $FF: bridge method | |
public Continuation create(Object var1, Continuation var2) { | |
return this.create((CoroutineScope)var1, var2); | |
} | |
} | |
// Test2Kt$main$time2$1$1.java | |
import kotlin.Metadata; | |
import kotlin.Unit; | |
import kotlin.coroutines.experimental.Continuation; | |
import kotlin.coroutines.experimental.intrinsics.IntrinsicsKt; | |
import kotlin.coroutines.experimental.jvm.internal.CoroutineImpl; | |
import kotlin.jvm.functions.Function2; | |
import kotlin.jvm.internal.Intrinsics; | |
import kotlinx.coroutines.experimental.CoroutineScope; | |
import kotlinx.coroutines.experimental.Deferred; | |
import org.jetbrains.annotations.NotNull; | |
import org.jetbrains.annotations.Nullable; | |
@Metadata( | |
mv = {1, 1, 5}, | |
bv = {1, 0, 1}, | |
k = 3, | |
d1 = {"\u0000\u000e\n\u0000\n\u0002\u0010\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u0010\u0000\u001a\u00020\u0001*\u00020\u0002H\u008a@ø\u0001\u0000¢\u0006\u0004\b\u0003\u0010\u0004"}, | |
d2 = {"<anonymous>", "", "Lkotlinx/coroutines/experimental/CoroutineScope;", "invoke", "(Lkotlinx/coroutines/experimental/CoroutineScope;Lkotlin/coroutines/experimental/Continuation;)Ljava/lang/Object;"} | |
) | |
final class Test2Kt$main$time2$1$1 extends CoroutineImpl implements Function2 { | |
private CoroutineScope p$; | |
private Object L$0; | |
private int I$0; | |
// $FF: synthetic field | |
final Deferred $one; | |
// $FF: synthetic field | |
final Deferred $two; | |
// $FF: synthetic method | |
// $FF: bridge method | |
public Object invoke(Object var1, Object var2) { | |
return this.invoke((CoroutineScope)var1, (Continuation)var2); | |
} | |
@Nullable | |
public final Object doResume(@Nullable Object data, @Nullable Throwable throwable) { | |
Object var10000; | |
StringBuilder var5; | |
int var6; | |
label29: { | |
Object var8 = IntrinsicsKt.getCOROUTINE_SUSPENDED(); | |
Deferred var9; | |
switch(super.label) { | |
case 0: | |
if(throwable != null) { | |
throw throwable; | |
} | |
CoroutineScope var3 = this.p$; | |
var5 = (new StringBuilder()).append("The answer is "); | |
var9 = this.$one; | |
this.L$0 = var5; | |
super.label = 1; | |
var10000 = var9.await(this); | |
if(var10000 == var8) { | |
return var8; | |
} | |
break; | |
case 1: | |
var5 = (StringBuilder)this.L$0; | |
if(throwable != null) { | |
throw throwable; | |
} | |
var10000 = data; | |
break; | |
case 2: | |
var6 = this.I$0; | |
var5 = (StringBuilder)this.L$0; | |
if(throwable != null) { | |
throw throwable; | |
} | |
var10000 = data; | |
break label29; | |
default: | |
throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine"); | |
} | |
Object var10 = var10000; | |
var6 = ((Number)var10).intValue(); | |
var5 = var5; | |
var9 = this.$two; | |
this.L$0 = var5; | |
this.I$0 = var6; | |
super.label = 2; | |
var10000 = var9.await(this); | |
if(var10000 == var8) { | |
return var8; | |
} | |
} | |
Object var7 = var10000; | |
String var4 = var5.append(var6 + ((Number)var7).intValue()).toString(); | |
System.out.println(var4); | |
return Unit.INSTANCE; | |
} | |
Test2Kt$main$time2$1$1(Deferred var1, Deferred var2, Continuation var3) { | |
super(2, var3); | |
this.$one = var1; | |
this.$two = var2; | |
} | |
@NotNull | |
public final Continuation create(@NotNull CoroutineScope $receiver, @NotNull Continuation $continuation) { | |
Intrinsics.checkParameterIsNotNull($receiver, "$receiver"); | |
Intrinsics.checkParameterIsNotNull($continuation, "$continuation"); | |
Test2Kt$main$time2$1$1 var3 = new Test2Kt$main$time2$1$1(this.$one, this.$two, $continuation); | |
var3.p$ = $receiver; | |
return var3; | |
} | |
@Nullable | |
public final Object invoke(@NotNull CoroutineScope $receiver, @NotNull Continuation $continuation) { | |
Intrinsics.checkParameterIsNotNull($receiver, "$receiver"); | |
Intrinsics.checkParameterIsNotNull($continuation, "$continuation"); | |
return ((Test2Kt$main$time2$1$1)this.create($receiver, $continuation)).doResume(Unit.INSTANCE, (Throwable)null); | |
} | |
// $FF: synthetic method | |
// $FF: bridge method | |
public Continuation create(Object var1, Continuation var2) { | |
return this.create((CoroutineScope)var1, var2); | |
} | |
} | |
// Test2Kt$main$time2$1$one$1.java | |
import kotlin.Metadata; | |
import kotlin.Unit; | |
import kotlin.coroutines.experimental.Continuation; | |
import kotlin.coroutines.experimental.intrinsics.IntrinsicsKt; | |
import kotlin.coroutines.experimental.jvm.internal.CoroutineImpl; | |
import kotlin.jvm.functions.Function2; | |
import kotlin.jvm.internal.Intrinsics; | |
import kotlinx.coroutines.experimental.CoroutineScope; | |
import org.jetbrains.annotations.NotNull; | |
import org.jetbrains.annotations.Nullable; | |
@Metadata( | |
mv = {1, 1, 5}, | |
bv = {1, 0, 1}, | |
k = 3, | |
d1 = {"\u0000\u000e\n\u0000\n\u0002\u0010\b\n\u0002\u0018\u0002\n\u0002\b\u0002\u0010\u0000\u001a\u00020\u0001*\u00020\u0002H\u008a@ø\u0001\u0000¢\u0006\u0004\b\u0003\u0010\u0004"}, | |
d2 = {"<anonymous>", "", "Lkotlinx/coroutines/experimental/CoroutineScope;", "invoke", "(Lkotlinx/coroutines/experimental/CoroutineScope;Lkotlin/coroutines/experimental/Continuation;)Ljava/lang/Object;"} | |
) | |
final class Test2Kt$main$time2$1$one$1 extends CoroutineImpl implements Function2 { | |
private CoroutineScope p$; | |
// $FF: synthetic method | |
// $FF: bridge method | |
public Object invoke(Object var1, Object var2) { | |
return this.invoke((CoroutineScope)var1, (Continuation)var2); | |
} | |
@Nullable | |
public final Object doResume(@Nullable Object var1, @Nullable Throwable var2) { | |
Object var4 = IntrinsicsKt.getCOROUTINE_SUSPENDED(); | |
switch(super.label) { | |
case 0: | |
if(var2 != null) { | |
throw var2; | |
} | |
CoroutineScope var3 = this.p$; | |
return Integer.valueOf(Test2Kt.meaninglessCounter()); | |
default: | |
throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine"); | |
} | |
} | |
Test2Kt$main$time2$1$one$1(Continuation var1) { | |
super(2, var1); | |
} | |
@NotNull | |
public final Continuation create(@NotNull CoroutineScope $receiver, @NotNull Continuation $continuation) { | |
Intrinsics.checkParameterIsNotNull($receiver, "$receiver"); | |
Intrinsics.checkParameterIsNotNull($continuation, "$continuation"); | |
Test2Kt$main$time2$1$one$1 var3 = new Test2Kt$main$time2$1$one$1($continuation); | |
var3.p$ = $receiver; | |
return var3; | |
} | |
@Nullable | |
public final Object invoke(@NotNull CoroutineScope $receiver, @NotNull Continuation $continuation) { | |
Intrinsics.checkParameterIsNotNull($receiver, "$receiver"); | |
Intrinsics.checkParameterIsNotNull($continuation, "$continuation"); | |
return ((Test2Kt$main$time2$1$one$1)this.create($receiver, $continuation)).doResume(Unit.INSTANCE, (Throwable)null); | |
} | |
// $FF: synthetic method | |
// $FF: bridge method | |
public Continuation create(Object var1, Continuation var2) { | |
return this.create((CoroutineScope)var1, var2); | |
} | |
} | |
// Test2Kt.java | |
import kotlin.Metadata; | |
import kotlin.coroutines.experimental.Continuation; | |
import kotlin.coroutines.experimental.CoroutineContext; | |
import kotlin.jvm.functions.Function2; | |
import kotlin.jvm.internal.Intrinsics; | |
import kotlinx.coroutines.experimental.BuildersKt; | |
import kotlinx.coroutines.experimental.CoroutineStart; | |
import kotlinx.coroutines.experimental.Deferred; | |
import kotlinx.coroutines.experimental.DeferredKt; | |
import org.jetbrains.annotations.NotNull; | |
@Metadata( | |
mv = {1, 1, 5}, | |
bv = {1, 0, 1}, | |
k = 2, | |
d1 = {"\u0000\u001a\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u0011\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0010\b\n\u0000\u001a\u0019\u0010\u0000\u001a\u00020\u00012\f\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¢\u0006\u0002\u0010\u0005\u001a\u0006\u0010\u0006\u001a\u00020\u0007¨\u0006\b"}, | |
d2 = {"main", "", "args", "", "", "([Ljava/lang/String;)V", "meaninglessCounter", "", "production sources for module test2_main"} | |
) | |
public final class Test2Kt { | |
public static final int meaninglessCounter() { | |
int counter = 0; | |
long i = (long)1; | |
long var3 = 10000000000L; | |
if(i <= var3) { | |
while(true) { | |
++counter; | |
if(i == var3) { | |
break; | |
} | |
++i; | |
} | |
} | |
return counter; | |
} | |
public static final void main(@NotNull String[] args) { | |
Intrinsics.checkParameterIsNotNull(args, "args"); | |
long start$iv = System.currentTimeMillis(); | |
Deferred one = DeferredKt.async$default((CoroutineContext)null, (CoroutineStart)null, (Function2)(new Test2Kt$main$time2$1$one$1((Continuation)null)), 3, (Object)null); | |
Deferred two = DeferredKt.async$default((CoroutineContext)null, (CoroutineStart)null, (Function2)(new Test2Kt$main$time2$1$two$1((Continuation)null)), 3, (Object)null); | |
BuildersKt.runBlocking$default((CoroutineContext)null, (Function2)(new Test2Kt$main$time2$1$1(one, two, (Continuation)null)), 1, (Object)null); | |
long time2 = System.currentTimeMillis() - start$iv; | |
String var9 = "Concurrent completed in " + time2 + " ms" + "\n"; | |
System.out.println(var9); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment