Skip to content

Instantly share code, notes, and snippets.

@yuzd
Created November 20, 2022 14:01
Show Gist options
  • Save yuzd/cf67048777f0eb8fc1b3757f5bf9e8f3 to your computer and use it in GitHub Desktop.
Save yuzd/cf67048777f0eb8fc1b3757f5bf9e8f3 to your computer and use it in GitHub Desktop.
package coroutines;
import kotlin.ResultKt;
import kotlin.Unit;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.EmptyCoroutineContext;
import kotlin.coroutines.intrinsics.IntrinsicsKt;
import kotlin.coroutines.jvm.internal.ContinuationImpl;
import kotlinx.coroutines.BuildersKt;
import kotlinx.coroutines.DelayKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author zdyu
* @create 2022-11-20 17:15
*/
@SuppressWarnings("rawtypes")
public class CoroutinesTest {
public static void main(String[] args) throws InterruptedException {
BuildersKt.runBlocking(EmptyCoroutineContext.INSTANCE, (coroutineScope, continuation) -> {
CoroutineMain mainCoroutine = new CoroutineMain(continuation);
return mainCoroutine.invokeSuspend(Unit.INSTANCE);
});
}
public static Object test1(Continuation continuation) {
CoroutineTest1 continuationTest1;
label20:
{
if (continuation instanceof CoroutineTest1) {
continuationTest1 = (CoroutineTest1) continuation;
int i = continuationTest1.label & Integer.MIN_VALUE;
if (i != 0) {
continuationTest1.label -= Integer.MIN_VALUE;
}
break label20;
}
continuationTest1 = new CoroutineTest1(continuation);
}
Object result = (continuationTest1).result;
Object var4 = IntrinsicsKt.getCOROUTINE_SUSPENDED();
String var1;
switch ((continuationTest1).label) {
case 0:
ResultKt.throwOnFailure(result);
var1 = "test1-start";
System.out.println(var1);
(continuationTest1).label = 1;
if (test2(continuationTest1) == var4) {
return var4;
}
break;
case 1:
ResultKt.throwOnFailure(result);
break;
default:
throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
}
var1 = "test1-end";
System.out.println(var1);
return Unit.INSTANCE;
}
final static class CoroutineTest1 extends ContinuationImpl {
Object result;
int label;
public CoroutineTest1(@Nullable Continuation<Object> completion) {
super(completion);
}
@Nullable
public Object invokeSuspend(@NotNull Object $result) {
this.result = $result;
this.label |= Integer.MIN_VALUE;
return test1(this);
}
}
public static Object test2(Continuation continuation) {
CoroutineTest2 continuationTest2;
label20:
{
if (continuation instanceof CoroutineTest2) {
continuationTest2 = (CoroutineTest2) continuation;
int i = continuationTest2.label & Integer.MIN_VALUE;
if (i != 0) {
continuationTest2.label -= Integer.MIN_VALUE;
}
break label20;
}
continuationTest2 = new CoroutineTest2(continuation);
}
Object result = (continuationTest2).result;
Object var4 = IntrinsicsKt.getCOROUTINE_SUSPENDED();
String var1;
switch ((continuationTest2).label) {
case 0:
ResultKt.throwOnFailure(result);
var1 = "test2-start";
System.out.println(var1);
(continuationTest2).label = 1;
if (DelayKt.delay(5000L, continuationTest2) == var4) {
return var4;
}
break;
case 1:
ResultKt.throwOnFailure(result);
break;
default:
throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
}
var1 = "test2-end";
System.out.println(var1);
return Unit.INSTANCE;
}
final static class CoroutineTest2 extends ContinuationImpl {
Object result;
int label;
public CoroutineTest2(@Nullable Continuation<Object> completion) {
super(completion);
}
@Nullable
public Object invokeSuspend(@NotNull Object $result) {
this.result = $result;
this.label |= Integer.MIN_VALUE;
return test2(this);
}
}
public static Object main(Continuation continuation) {
CoroutineMain continuationMain;
label20:
{
if (continuation instanceof CoroutineMain) {
continuationMain = (CoroutineMain) continuation;
int i = continuationMain.label & Integer.MIN_VALUE;
if (i != 0) {
continuationMain.label -= Integer.MIN_VALUE;
}
break label20;
}
continuationMain = new CoroutineMain(continuation);
}
Object result = (continuationMain).result;
Object var3 = IntrinsicsKt.getCOROUTINE_SUSPENDED();
String var2;
switch (continuationMain.label) {
case 0:
ResultKt.throwOnFailure(result);
var2 = "main-start";
System.out.println(var2);
continuationMain.label = 1;
if (test1(continuationMain) == var3) {
return var3;
}
break;
case 1:
ResultKt.throwOnFailure(result);
break;
default:
throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
}
var2 = "main-end";
System.out.println(var2);
return Unit.INSTANCE;
}
final static class CoroutineMain extends ContinuationImpl {
Object result;
int label;
public CoroutineMain(@Nullable Continuation<Object> completion) {
super(completion);
}
@Nullable
public Object invokeSuspend(@NotNull Object $result) {
this.result = $result;
this.label |= Integer.MIN_VALUE;
return main(this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment