暂停功能在Kotlin协程中意味着什么? [英] What does the suspend function mean in a Kotlin Coroutine?

查看:207
本文介绍了暂停功能在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 ?

我们看到awaitcomputation上被调用,因此可能是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<T>的附加参数.例如,上面的函数将由编译器转换为:

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<T>是一个包含两个函数的接口,这些函数被调用以使用返回值或异常(如果在函数暂停期间发生错误)来恢复协程.

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天全站免登陆