Kotlin 协程中的挂起函数是什么意思? [英] What does the suspend function mean in a Kotlin Coroutine?

查看:70
本文介绍了Kotlin 协程中的挂起函数是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Kotlin Coroutine 并且知道它基于 suspend 函数.但是suspend是什么意思?

I'm reading Kotlin Coroutine and know that it is based on suspend function. But what does suspend mean?

协程或函数被挂起?

来自 https://kotlinlang.org/docs/reference/coroutines.html

基本上,协程是可以在不阻塞线程的情况下挂起的计算

Basically, coroutines are computations that can be suspended without blocking a thread

我听到人们常说暂停功能".但我认为是协程因为等待函数完成而被暂停?暂停"通常意味着停止操作",在这种情况下协程是空闲的.

I heard people often say "suspend function". But I think it is the coroutine who gets suspended because it is waiting for the function to finished? "suspend" usually means "cease operation", in this case the coroutine is idle.

我们应该说协程被暂停了吗?

Should we say the coroutine is suspended ?

哪个协程被暂停?

来自 https://kotlinlang.org/docs/reference/coroutines.html

继续类比,await() 可以是一个挂起函数(因此也可以从 async {} 块中调用),它会挂起协程,直到某些计算完成并返回其结果:

To continue the analogy, await() can be a suspending function (hence also callable from within an async {} block) that suspends a coroutine until some computation is done and returns its result:

async { // Here I call it the outer async coroutine
    ...
    // Here I call computation the inner coroutine
    val result = computation.await()
    ...
}

它说在某些计算完成之前挂起协程",但协程就像一个轻量级线程.那么如果协程挂了,怎么计算呢?

It says "that suspends a coroutine until some computation is done", but coroutine is like a lightweight thread. So if the coroutine is suspended, how can the computation is done ?

我们看到在computation上调用了await,所以可能是async返回了Deferred,这意味着它可以启动另一个协程

We see await is called on computation, so it might be async that returns Deferred, which means it can start another coroutine

fun computation(): Deferred<Boolean> {
    return async {
        true
    }
}

引用说挂起协程.是指suspend外层async协程,还是suspend内层computation协程?

The quote say that suspends a coroutine. Does it mean suspend the outer async coroutine, or suspend the inner computation coroutine?

suspend 是否意味着外部 async 协程正在等待 (await) 内部 computation 协程完成,它(外部 async 协程)空闲(因此得名挂起)并将线程返回到线程池,当子 computation 协程完成时,它(外部async 协程)唤醒,从池中取出另一个线程并继续?

Does suspend mean that while outer async coroutine is waiting (await) for the inner computation coroutine to finish, it (the outer async coroutine) idles (hence the name suspend) and returns thread to the thread pool, and when the child computation coroutine finishes, it (the outer async coroutine) wakes up, takes another thread from the pool and continues?

我提到这个线程的原因是因为 https://kotlinlang.org/docs/tutorials/coroutines-basic-jvm.html

The reason I mention the thread is because of https://kotlinlang.org/docs/tutorials/coroutines-basic-jvm.html

协程在等待时线程返回到池中,等待完成后协程在池中的空闲线程上恢复

The thread is returned to the pool while the coroutine is waiting, and when the waiting is done, the coroutine resumes on a free thread in the pool

推荐答案

挂起函数是所有协程的核心.挂起函数只是一个可以在以后暂停和恢复的函数.他们可以执行长时间运行的操作并等待它完成而不会阻塞.

Suspending functions are at the center of everything coroutines. A suspending function is simply a function that can be paused and resumed at a later time. They can execute a long running operation and wait for it to complete without blocking.

挂起函数的语法与常规函数的语法相似,只是添加了suspend 关键字.它可以接受一个参数并有一个返回类型.但是,挂起函数只能由另一个挂起函数或在协程中调用.

The syntax of a suspending function is similar to that of a regular function except for the addition of the suspend keyword. It can take a parameter and have a return type. However, suspending functions can only be invoked by another suspending function or within a coroutine.

suspend fun backgroundTask(param: Int): Int {
     // long running operation
}

在幕后,编译器将挂起函数转换为另一个没有 suspend 关键字的函数,该函数采用 Continuation 类型的附加参数.例如上面的函数,会被编译器转换成这样:

Under the hood, suspend functions are converted by the compiler to another function without the suspend keyword, that takes an addition parameter of type Continuation<T>. The function above for example, will be converted by the compiler to this:

fun backgroundTask(param: Int, callback: Continuation<Int>): Int {
   // long running operation
}

Continuation 是一个接口,它包含两个函数,如果函数挂起时发生错误,则调用这些函数以恢复协程并返回值或异常.

Continuation<T> is an interface that contains two functions that are invoked to resume the coroutine with a return value or with an exception if an error had occurred while the function was suspended.

interface Continuation<in T> {
   val context: CoroutineContext
   fun resume(value: T)
   fun resumeWithException(exception: Throwable)
}

这篇关于Kotlin 协程中的挂起函数是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆