我正在读Kotlin Coroutine并知道它是基于suspend功能的.但是什么suspend意思呢?
协程或功能被暂停?
来自https://kotlinlang.org/docs/reference/coroutines.html
协同程序基本上是协程,可以在不阻塞线程的情况下暂停
我听说人们经常说"暂停功能".但是我认为正在等待函数完成的是协同程序被暂停?"暂停"通常意味着"停止操作",在这种情况下,协程是空闲的.
我们应该说协程被暂停吗?
哪个协程被暂停?
来自https://kotlinlang.org/docs/reference/coroutines.html
为了继续这个类比,await()可以是一个挂起函数(因此也可以从async {}块中调用),它挂起一个协同程序,直到完成一些计算并返回其结果:
async { // Here I call it the outer async coroutine
...
// Here I call computation the inner coroutine
val result = computation.await()
...
}
Run Code Online (Sandbox Code Playgroud)
它说"在完成某些计算之前暂停协程",但协同程序就像一个轻量级的线程.因此,如果协程被暂停,那么计算怎么办呢?
我们看到await被调用computation,所以它可能会async返回Deferred,这意味着它可以启动另一个协同程序
fun computation(): Deferred<Boolean> {
return async {
true
}
}
Run Code Online (Sandbox Code Playgroud)
引述说暂停协程.它是指suspend外部的asynccoroutine,还是suspend内部的computationcoroutine?
这suspend意味着当外部async协程正在wait(await …