Anko协程kotlin中的Deferred是什么?

What is Deferred in Anko coroutines kotlin?

在 kotlin 的 Anko 协程库中,其中有一项功能 bg() 可以轻松地在后台线程上执行您的代码。 return 类型是 Deferred。那什么是Deferred?

引用Link

(1) https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Deferred.kt

(2) https://github.com/Kotlin/anko/wiki/Anko-Coroutines#bg

  fun getData(): Data { ... }
  fun showData(data: Data) { ... }

  async(UI) {
      val data: Deferred<Data> = bg {
      // Runs in background
      getData()
      }

      // This code is executed on the UI thread
      showData(data.await())
  }

如果你打扰了,我首先要引用问题中第一个 link Deferred class 文档中的第一句话:

Deferred value is a non-blocking cancellable future.

事实上,deferred 是futurepromise (see this wikipedia article) 的同义词。

Deferred class 是 kotlinx-coroutines project that provides library support for Kotlin coroutines. The recommended way to start learning more about it is by reading this guide 的一部分。