Kotlin - 掛起函數(shù)(2)- 本質(zhì)

本質(zhì)

掛起函數(shù)為什么能被掛起恢復(fù)呢?難道靠suspend關(guān)鍵字描述函數(shù)就可以了?其實本質(zhì)還是回調(diào),只不過Kotlin編譯器幫我們實現(xiàn)了而已。
將以下代碼反編譯看看

//定義掛起函數(shù)
suspend fun test() {
    val resultA = getA()
    val resultB = getB()
}

suspend fun getA(): String {
    return "A"
}

suspend fun getB(): String {
    return "B"
}

    
//將getA函數(shù)反編譯看看
public final Object getA(@NotNull Continuation<String> $completion) {//2
    //執(zhí)行耗時任務(wù)
    return "A";
}
  • 注釋1:定義了掛起函數(shù)getName;
  • 注釋2:反編譯后看到,suspend關(guān)鍵字沒有了,函數(shù)返回值為Object,增加入?yún)ontinuation<String> $completion,泛型String為函數(shù)返回值;
Continuation是什么玩意?
public interface Continuation<in T> {//1
    /**
     * The context of the coroutine that corresponds to this continuation.
     */
    public val context: CoroutineContext//2

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

  • 注釋1:Continuation是一個接口,是不是有點回調(diào)的味道;
  • 注釋2:維護協(xié)程上下文(Dispatchers、異常處理器等);
  • 注釋3:回調(diào)接口,將結(jié)果回調(diào)到掛起點,然后執(zhí)行后續(xù)的代碼;
Java訪問掛起函數(shù)
public void test(){
    new A().getA(new Continuation<String>() {
        @NonNull
        @Override
        public CoroutineContext getContext() {
            return null;
        }

        @Override
        public void resumeWith(@NonNull Object o) {

        }
    });
}

是不是又看到了回調(diào)的影子了。

那么具體協(xié)程是怎么實現(xiàn)掛起跟恢復(fù)的呢?有點復(fù)雜,有興趣的可以關(guān)注下后續(xù)的分析。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容