Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Briefly Introduction of Kotlin coroutines
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Elvis Lin
October 19, 2018
Programming
330
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Briefly Introduction of Kotlin coroutines
Introduce basic concepts of Kotlin coroutines
Elvis Lin
October 19, 2018
More Decks by Elvis Lin
See All by Elvis Lin
Protect Users' Privacy in iOS 14
elvismetaphor
0
65
Dubugging Tips and Tricks for iOS development
elvismetaphor
0
80
Strategies of Facebook LightSpeed project
elvismetaphor
0
130
Background Execution And WorkManager
elvismetaphor
2
550
作為一個跨平台的 Mobile App 開發者,從入門到放棄!?
elvismetaphor
2
580
Dependency Injection for testability of iOS app
elvismetaphor
1
1.5k
MotionLayout Brief Introduction
elvismetaphor
1
390
Chapter 10. Pattern Matching with Regular Expressions
elvismetaphor
0
82
Machine Learning Application on Mobile Devices
elvismetaphor
0
160
Other Decks in Programming
See All in Programming
Java 27新機能 / Java 27 new features
kishida
2
200
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
930
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
1.1k
ソニーのクラウド共通基盤の変遷とAI時代の開発スタイルに合わせた進化 / The Journey of Sony’s Common Cloud Platform and Its Evolution for AI-Native Development
kenjiyoneyama
0
240
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
5k
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Rails World 2026, Austin, Texas
marcoroth
0
190
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
120
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
350
プロダクトコードからライブラリの境界を見つける
elmetal
PRO
0
110
Androidだけじゃない、DroidKaigiで広がった私の技術スタック
akkeylab
0
120
残高管理から台帳サービスへの進化
artoy
0
170
なぜCTOを降りてFDEを選んだのか?〜なぜプロダクト企業がFDEで顧客の現場に踏み込むのか〜
gonta
1
160
Featured
See All Featured
How to Talk to Developers About Accessibility
jct
2
560
Building a Scalable Design System with Sketch
lauravandoore
464
34k
Mobile First: as difficult as doing things right
swwweet
225
10k
We Have a Design System, Now What?
morganepeng
55
8.4k
Automating Front-end Workflow
addyosmani
1369
210k
Discover your Explorer Soul
emna__ayadi
2
1.3k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
3.3k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
76k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.1k
Rails Girls Zürich Keynote
gr2m
96
14k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
350
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
910
Transcript
Kotlin Coroutines, 從入⾨門到入⾨門 Elvis Lin(@elvismetaphor) Taiwan 2018 #JCConf
關於我 • Elvis Lin • Android, iOS 與 React Native
永遠的初學者 • Twitter: @elvismetaphor • Blog: https://blog.elvismetaphor.me #JCConf Taiwan 2018
⼤大綱 • 循序處理理 • 並⾏行行處理理與Multi-thread • Why Coroutines? • 基本的使⽤用⽅方式
#JCConf Taiwan 2018
循序處理理 #JCConf Taiwan 2018 Main
循序處理理的程式 fun main(args: Array<String>) { println("First") println("Second") println(“Third") } #JCConf
Taiwan 2018
循序處理理的程式與Thread fun main(args: Array<String>) { println("First") printDelayed(“Second") println("Third") } fun
printDelayed(message: String) { Thread.sleep(1000) println(message) } #JCConf Taiwan 2018
循序處理理的程式與 Coroutines fun main(args: Array<String>) { println(“First") runBlocking { printDelay("Second")
} println("Third") } suspend fun printDelay(message: String) { delay(1000) println(message) } #JCConf Taiwan 2018
循序處理理的程式 與 Coroutines(簡易易版) fun main(args: Array<String>) { println(“First") runBlocking {
delay(1000) println(“Second”) } println("Third") } #JCConf Taiwan 2018
並⾏行行處理理 (Multi-thread) #JCConf Taiwan 2018 Main Other
並⾏行行處理理的程式與 Thread fun main(args: Array<String>) { println("First") printNonBlocking("Second") println("Third") }
fun printNonBlocking(message: String) { Thread { Thread.sleep(1000) println(message) }.start() } // Output: First Third Second
並⾏行行處理理的程式與 coroutines fun main(args: Array<String>) { println("First") launch { printDelay("Second")
} println("Three") } suspend fun printDelay(message: String) { delay(1000) println(message) } // Output: First Third Second #JCConf Taiwan 2018
Why Coroutines #JCConf Taiwan 2018
Coroutines • Coroutines are like very light-weight thread • Coroutines
computations can be done without blocking other threads. • Coroutine 的執⾏行行可以更更有效地利利⽤用原本已經建立在 ThreadPool 裡⾯面的 Threads #JCConf Taiwan 2018
常⽤用的關鍵字 • suspend • delay • launch • runBlocking •
await • async #JCConf Taiwan 2018
suspend • ⼀一個 modifier • ⼀一個 suspend 的 function 只可以被
suspend function 或另⼀一 個 coroutine 呼叫 #JCConf Taiwan 2018
launch • 執⾏行行⼀一個 async 的 job #JCConf Taiwan 2018 launch
{ anSimpleFunction() }
runBlocking • 執⾏行行⼀一個 sync 的 job #JCConf Taiwan 2018 runBlocking
{ anSimpleFunction() }
async & await • aysnc {…} 會回傳⼀一個 Deferred<T> • 在回傳的
deferred 上⾯面加上 await 會獲得值 #JCConf Taiwan 2018 val value = async {calculate(1)}.await()
Live Demo https://gist.github.com/elvismetaphor/ 810712e3a08863c963c871f1bca0b3f2 #JCConf Taiwan 2018
Coroutines v.s. RxJava • Coroutines: 更更輕量量,更更容易易將原本循序式的程式改成非同 步、並⾏行行處理理的⽅方式 • RxJava: 提供更更豐富的
operators 做 stream 的轉換 • 根據你的使⽤用情境挑選適合的 library #JCConf Taiwan 2018
總結 • Coroutine 提供⼀一個類似 Thread 的功能,但是更更輕量量的操 作 • Coroutine 讓你可以容易易的將原本同步操作的邏輯轉換成非
同步操作的邏輯 #JCConf Taiwan 2018
參參考資料 • 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
Thank you :) #JCConf Taiwan 2018