Slide 1

Slide 1 text

Kotlin Coroutines, 從入⾨門到入⾨門 Elvis Lin(@elvismetaphor) Taiwan 2018 #JCConf

Slide 2

Slide 2 text

關於我 • Elvis Lin • Android, iOS 與 React Native 永遠的初學者 • Twitter: @elvismetaphor • Blog: https://blog.elvismetaphor.me #JCConf Taiwan 2018

Slide 3

Slide 3 text

⼤大綱 • 循序處理理 • 並⾏行行處理理與Multi-thread • Why Coroutines? • 基本的使⽤用⽅方式 #JCConf Taiwan 2018

Slide 4

Slide 4 text

循序處理理 #JCConf Taiwan 2018 Main

Slide 5

Slide 5 text

循序處理理的程式 fun main(args: Array) {
 println("First")
 println("Second")
 println(“Third")
 } #JCConf Taiwan 2018

Slide 6

Slide 6 text

循序處理理的程式與Thread fun main(args: Array) {
 println("First")
 printDelayed(“Second")
 println("Third")
 } fun printDelayed(message: String) {
 Thread.sleep(1000)
 println(message)
 } #JCConf Taiwan 2018

Slide 7

Slide 7 text

循序處理理的程式與 Coroutines fun main(args: Array) {
 println(“First")
 runBlocking {
 printDelay("Second")
 }
 println("Third")
 } suspend fun printDelay(message: String) {
 delay(1000)
 println(message)
 } #JCConf Taiwan 2018

Slide 8

Slide 8 text

循序處理理的程式
 與 Coroutines(簡易易版) fun main(args: Array) {
 println(“First")
 runBlocking {
 delay(1000)
 println(“Second”)
 }
 println("Third")
 } #JCConf Taiwan 2018

Slide 9

Slide 9 text

並⾏行行處理理 (Multi-thread) #JCConf Taiwan 2018 Main Other

Slide 10

Slide 10 text

並⾏行行處理理的程式與 Thread fun main(args: Array) {
 println("First")
 printNonBlocking("Second")
 println("Third")
 } fun printNonBlocking(message: String) {
 Thread {
 Thread.sleep(1000)
 println(message)
 }.start()
 } // Output:
 First
 Third
 Second

Slide 11

Slide 11 text

並⾏行行處理理的程式與 coroutines fun main(args: Array) {
 println("First")
 launch {
 printDelay("Second")
 }
 println("Three")
 } suspend fun printDelay(message: String) {
 delay(1000)
 println(message)
 } // Output:
 First
 Third
 Second #JCConf Taiwan 2018

Slide 12

Slide 12 text

Why Coroutines #JCConf Taiwan 2018

Slide 13

Slide 13 text

Coroutines • Coroutines are like very light-weight thread • Coroutines computations can be done without blocking other threads. • Coroutine 的執⾏行行可以更更有效地利利⽤用原本已經建立在 ThreadPool 裡⾯面的 Threads #JCConf Taiwan 2018

Slide 14

Slide 14 text

常⽤用的關鍵字 • suspend • delay • launch • runBlocking • await • async #JCConf Taiwan 2018

Slide 15

Slide 15 text

suspend • ⼀一個 modifier • ⼀一個 suspend 的 function 只可以被 suspend function 或另⼀一 個 coroutine 呼叫 #JCConf Taiwan 2018

Slide 16

Slide 16 text

launch • 執⾏行行⼀一個 async 的 job #JCConf Taiwan 2018 launch {
 anSimpleFunction()
 }

Slide 17

Slide 17 text

runBlocking • 執⾏行行⼀一個 sync 的 job #JCConf Taiwan 2018 runBlocking {
 anSimpleFunction()
 }

Slide 18

Slide 18 text

async & await • aysnc {…} 會回傳⼀一個 Deferred • 在回傳的 deferred 上⾯面加上 await 會獲得值 #JCConf Taiwan 2018 val value = async {calculate(1)}.await()

Slide 19

Slide 19 text

Live Demo https://gist.github.com/elvismetaphor/ 810712e3a08863c963c871f1bca0b3f2 #JCConf Taiwan 2018

Slide 20

Slide 20 text

Coroutines v.s. RxJava • Coroutines: 更更輕量量,更更容易易將原本循序式的程式改成非同 步、並⾏行行處理理的⽅方式 • RxJava: 提供更更豐富的 operators 做 stream 的轉換 • 根據你的使⽤用情境挑選適合的 library #JCConf Taiwan 2018

Slide 21

Slide 21 text

總結 • Coroutine 提供⼀一個類似 Thread 的功能,但是更更輕量量的操 作 • Coroutine 讓你可以容易易的將原本同步操作的邏輯轉換成非 同步操作的邏輯 #JCConf Taiwan 2018

Slide 22

Slide 22 text

參參考資料 • https://kotlinlang.org/docs/reference/coroutines- overview.html • Introduction to coroutines: https://youtu.be/_hfBv0a09Jc • Exploring coroutines in Kotlin: https://youtu.be/ jT2gHPQ4Z1Q #JCConf Taiwan 2018

Slide 23

Slide 23 text

Thank you :) #JCConf Taiwan 2018