[안드로이드] 코루틴의

코루틴을 사용하면 suspend 함수를 통해 여러 진입점을 일시 중지-재개할 수 있습니다.

이를 통해 하위 프로그램을 번갈아 실행할 수 있습니다.

어떻게 가능합니까?

CPS(Continuation Passing Style) 패러다임사용

기본 원칙은 콜백 인터페이스를 매개변수로 사용하여 연속 개체를 전달하는 것입니다.

매개변수를 받은 메서드는 일시 중단된 메서드를 다시 실행합니다.

먼저 작동 방식을 확인하십시오. 일시 중단 기능을 만듭니다.

폭로하다 기능 getUserId(), getUserNickname() 붓다 번갈아 수행하다.

runBlocking {
    val one = async { getUserId() }
    val two = async { getUserNickname() }

    println("결과값 :  ${one.await()} + ${two.await()} ")
}

suspend fun getUserId() : String{
    delay(1000L)
    return "myid1"
}

suspend fun getUserNickname() :String{
    delay(500L)
    return "hong"
}

폭로하다 기능 getUserNickname() ~에서 동작 원리 알아내다 디컴파일 그렇습니다

@Nullable
public final Object getUserNickname(@NotNull Continuation var1) { 		#1
   Object $continuation;
   label20: {
      if (var1 instanceof <undefinedtype>) {
         $continuation = (<undefinedtype>)var1;
         if ((((<undefinedtype>)$continuation).label & Integer.MIN_VALUE) !
= 0) { ((<undefinedtype>)$continuation).label -= Integer.MIN_VALUE; break label20; } } $continuation = new ContinuationImpl(var1) { #2 // $FF: synthetic field Object result; int label; @Nullable public final Object invokeSuspend(@NotNull Object $result) { this.result = $result; this.label |= Integer.MIN_VALUE; return MainActivity.this.getUserNickname(this); } }; } Object $result = ((<undefinedtype>)$continuation).result; Object var4 = IntrinsicsKt.getCOROUTINE_SUSPENDED(); switch(((<undefinedtype>)$continuation).label) { #3 case 0: ResultKt.throwOnFailure($result); ((<undefinedtype>)$continuation).label = 1; #4 if (DelayKt.delay(500L, (Continuation)$continuation) == var4) { return var4; } break; case 1: ResultKt.throwOnFailure($result); break; default: throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine"); } return "hong"; }

변경된 부분

1. Suspend가 제거되고 getUserNickname() 함수 매개변수에 연속 구현 개체가 추가된 것을 확인할 수 있습니다.

2. $continuation 객체 초기화 함수 -> 실행 정보(result, label)를 담는 콜백 함수가 생성된다.

3.label은 switch-case로 기능을 분류하여 수행합니다.

4. 해당 레이블이 실행되면 레이블이 증가합니다.

Continuation의 구현 객체는 아래의 Continuation 인터페이스를 구현하고 결과 값을 매개변수로 하는 resume 함수를 갖는다.

그리고 관련된 코루틴 문맥붓다 포함 CoroutineContext붓다 가지다.

/**
 * Interface representing a continuation after a suspension point that returns a value of type `T`.
 */
@SinceKotlin("1.3")
public interface Continuation<in T> {
    /**
     * The context of the coroutine that corresponds to this continuation.
     */
    public val context: CoroutineContext

    /**
     * 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>)
}

참조.

https://myungpyo.medium.com/coroutine-official-guide-more-read-part-1-dive-3-b174c735d4fa

https://jisungbin.medium.com/코루틴-cps-화신탐구하다-7b9cd5f5c7bd