Kotlin Coroutines 筆記 (一)

安靜的妹子.jpg

一. 協程

Kotlin 在1.1版本之後引入了協程的概念,目前它還是一個試驗的API。

在操作系統中,我們知道進程和線程的概念以及區別。而協程相比於線程更加輕量級,協程又稱微線程。

協程是一種用戶態的輕量級線程,協程的調度完全由用戶控制。協程擁有自己的寄存器上下文和棧。協程調度切換時,將寄存器上下文和棧保存到其他地方,在切回來的時候,恢復先前保存的寄存器上下文和棧,直接操作棧則基本沒有內核切換的開銷,可以不加鎖的訪問全局變量,所以上下文的切換非常快。

Kotlin 的協程是無阻塞的異步編程方式。Kotlin 允許我們使用協程來代替複雜的線程阻塞操作,並且複用原本的線程資源。

Kotlin 的協程是依靠編譯器實現的, 並不需要操作系統和硬件的支持。編譯器爲了讓開發者編寫代碼更簡單方便, 提供了一些關鍵字(例如suspend), 並在內部自動生成了一些支持型的代碼。

先舉兩個例子來說明協程的輕量級,分別創建10w個協程和10w個線程進行測試。

import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.runBlocking

/**
 * Created by tony on 2018/7/18.
 */
fun main(args: Array<String>) {

    val start = System.currentTimeMillis()

    runBlocking {
        val jobs = List(100000) {
            // 創建新的coroutine
            launch(CommonPool) {
                // 掛起當前上下文而非阻塞1000ms
                delay(1000)
                println("thread name="+Thread.currentThread().name)
            }
        }

        jobs.forEach {
            it.join()
        }
    }

    val spend = (System.currentTimeMillis()-start)/1000

    println("Coroutines: spend= $spend s")

}

10w個協程的創建在本機大約花費 1 秒,經過測試100w個協程的創建大約花費11 秒。

十萬個協程測試.jpeg
import kotlin.concurrent.thread

/**
 * Created by tony on 2018/7/18.
 */
fun main(args: Array<String>) {

    val start = System.currentTimeMillis()

    val threads = List(100000) {
        // 創建新的線程
        thread {
            Thread.sleep(1000)
            println(Thread.currentThread().name)
        }
    }

    threads.forEach { it.join() }

    val spend = (System.currentTimeMillis()-start)/1000

    println("Threads: spend= $spend s")

}
十萬個線程測試.jpeg

10w個線程的創建出現了OutOfMemoryError。

二. 協程常用的基本概念

2.1 CoroutineContext

協程上下文,它包含了一個默認的協程調度器。所有協程都必須在 CoroutineContext 中執行。

2.2 CoroutineDispatcher

協程調度器,它用來調度和處理任務,決定了相關協程應該在哪個或哪些線程中執行。Kotlin 的協程包含了多種協程調度器。

2.3 Continuation

按照字面意思是繼續、持續的意思。協程的執行可能是分段執行的:先執行一段,掛起,再執行一段,再掛起......

Continuation 則表示每一段執行的代碼,Continuation 是一個接口。

2.4 Job

任務執行的過程被封裝成 Job,交給協程調度器處理。Job 是一種具有簡單生命週期的可取消任務。Job 擁有三種狀態:isActive、isCompleted、isCancelled。

                                                      wait children
    +-----+       start      +--------+   complete   +-------------+  finish  +-----------+
    | New | ---------------> | Active | -----------> | Completing  | -------> | Completed |
    +-----+                  +--------+              +-------------+          +-----------+
       |                         |                         |
       | cancel                  | cancel                  | cancel
       V                         V                         |
  +-----------+   finish   +------------+                  |
  | Cancelled | <--------- | Cancelling | <----------------+
  |(completed)|            +------------+
  +-----------+

2.5 Deferred

Deferred 是 Job 的子類。Job 完成時是沒有返回值的,Deferred 可以爲任務完成時提供返回值,並且Deferred 新增了一個狀態 isCompletedExceptionally。

                                                    wait children
   +-----+       start      +--------+   complete  +-------------+ finish +-----------+
   | New | ---------------> | Active | ----------> | Completing  | ---+-> | Resolved  |
   +-----+                  +--------+             +-------------+    |   |(completed)|
      |                         |                        |            |   +-----------+
      | cancel                  | cancel                 | cancel     |
      V                         V                        |            |   +-----------+
 +-----------+   finish   +------------+                 |            +-> |  Failed   |
 | Cancelled | <--------- | Cancelling | <---------------+                |(completed)|
 |(completed)|            +------------+                                  +-----------+
 +-----------+

2.6 suspend 關鍵字

協程計算可以被掛起而無需阻塞線程。我們使用 suspend 關鍵字來修飾可以被掛起的函數。被標記爲 suspend 的函數只能運行在協程或者其他 suspend 函數中。

suspend 可以修飾普通函數、擴展函數和 lambda 表達式。

三. 協程的多種使用方式

Kotlin 的協程支持多種異步模型:


Kotlin協程支持的異步模型.png

這些異步機制在 Kotlin 的協程中都有實現。

Kotlin 官方對協程提供的三種級別的能力支持, 分別是: 最底層的語言層, 中間層標準庫(kotlin-stdlib), 以及最上層應用層(kotlinx.coroutines)。

3.1 協程的hello world版本

使用 launch 和 async 都能啓動一個新的協程。

    val job = launch {
        delay(1000)
        println("Hello World!")
    }

    Thread.sleep(2000)

或者

    val deferred  = async {

        delay(1000)
        println("Hello World!")
    }

    Thread.sleep(2000)

它們分別會返回一個 Job 對象和一個 Deferred 對象。

下面使用 runBlocking 來創建協程。

fun main(args: Array<String>) = runBlocking<Unit> {
    launch {
        delay(1000)
        println("Hello World!")
    }

    delay(2000)
}

runBlocking 創建的協程直接運行在當前線程上,同時阻塞當前線程直到結束。

launch 和 async 在創建時可以使用不同的CoroutineDispatcher,例如:CommonPool。

在 runBlocking 內還可以創建其他協程,例如launch。反之則不行。

總結:

Kotlin 的協程能夠簡化異步編程的代碼,使用同步的方式實現異步。協程的概念和理論比較多,第一篇只是一個開始,只整理了其中一些基本概念。

該系列的相關文章:
Kotlin Coroutines 筆記 (二)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章