Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
触って分かるコルーチン
Search
izmeal2000
February 13, 2019
0
600
触って分かるコルーチン
izmeal2000
February 13, 2019
Tweet
Share
More Decks by izmeal2000
See All by izmeal2000
実践、BFF ~ BFFはFiNCのアプリで何を解決したのか ~
kensukeizumi
6
5.1k
Androidの歩数取得とバックグラウンド制限
kensukeizumi
0
4.2k
Featured
See All Featured
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
Rails Girls Zürich Keynote
gr2m
94
13k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.2k
Documentation Writing (for coders)
carmenintech
67
4.5k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Faster Mobile Websites
deanohume
305
30k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
Side Projects
sachag
452
42k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
171
50k
Visualization
eitanlees
146
15k
Transcript
৮ͬͯΘ͔ΔCoroutine FiNC Technologies Inc. / Engineer Development Kensuke Izumi
ࣗݾհ Kensuke Izumi Android Engineer @FiNC Kotlin / javascript /
ruby Twitter: @izmeal2000
͍͔ʹͯ͠reject͞Εͨͷ͔
͍͔ʹͯ͠reject͞Εͨͷ͔ νʔϜͰͷෳճͷϨϏϡʔΛܦͯ ϒϥογϡΞοϓ͞ΕͨCFP!
͍͔ʹͯ͠reject͞Εͨͷ͔ ͗Γ͗Γ·Ͱ೪ͬͯमਖ਼ʹमਖ਼ΛॏͶͯSUBMIT!!! At 23:59 2018/10/21
͍͔ʹͯ͠reject͞Εͨͷ͔
͍͔ʹͯ͠reject͞Εͨͷ͔ Rejected by System
ΞδΣϯμ 1. Coroutineॳ৺ऀʹཱ͔ͪͩΔղͳఆٛͨͪ 2. Androidʹ͓͚ΔThreadͷ෮श 3. CoroutineͱThreadͷؔ 4. Coroutineͷղܾ͢Δ՝
ΞδΣϯμ 1. Coroutineॳ৺ऀʹཱ͔ͪͩΔղͳఆٛͨͪ 2. Androidʹ͓͚ΔThreadͷ෮श 3. CoroutineͱThreadͷؔ 4. Coroutineͷղܾ͢Δ՝
What is Coroutine?
What is Coroutine? Coroutines are light weight threads
What is Coroutine? Coroutines are light weight threads ?
ු͔ΜͰ͖ͨٙ Coroutines are light weight threads ίϧʔνϯܰྔεϨου
ු͔ΜͰ͖ͨٙ Coroutines are light weight threads CoroutineͬͯThreadͱରʹൺֱͯ͠ྑ͍ͷͳͷʁ ݁ہJVMεϨου্Ͱ࣮ߦ͞ΕΔͷͰʁ
ͦͦThreadͬͯɺɺɺ ͕Android։ൃΛ࢝Ίͨ࣌ͷঢ়گ ඇಉظॲཧʁɹRXʂ
ͦͦThreadͬͯɺɺɺ ͕Android։ൃΛ࢝Ίͨ࣌ͷঢ়گ ඇಉظॲཧʁɹRXʂ Thread? observeOnͱ͔Ͱࢦఆ͢Δͭʂ
Threadͷࡶͳೝࣝ Threadͷ෮श͔Β࢝ΊΑ͏
ΞδΣϯμ 1. Coroutineॳ৺ऀʹཱ͔ͪͩΔղͳఆٛͨͪ 2. Androidʹ͓͚ΔThreadͷ෮श 3. CoroutineͱThreadͷؔ 4. Coroutineͷղܾ͢Δ՝
Androidʹ͓͚ΔThreadͷ෮श Androidͷ̐ͭͷίϯϙʔωϯτͱϓϩηε ɾActivity ɾService ɾBroadcastReceiver ɾContent Providers ɹ (جຊతʹ) ΞϓϦҰͭͷϓϩηεΛىಈ͍ͯͯ͠ɺϚϧνεϨουͰಈ͘
Androidʹ͓͚ΔThreadͷ෮श Androidʹ͓͚ΔThreadૢ࡞ͷݪଇ 1. Do not block the UI thread ɹඳը͕ࢭ·ΔͱΫϦςΟΧϧ
2. Do not access the Android UI toolkit from outside the UI thread ɹUI toolkitεϨουηʔϑͰແ͍ ɹ ࢀߟɿhttps://developer.android.com/guide/components/processes-and-threads
21 Androidʹ͓͚ΔThreadͷ෮श Androidʹ͓͚ΔϚϧνεϨουͷ੍ ɾωοτϫʔΫॲཧDBͷॻ͖ࠐΈͳͲΛߦ͏ࡍʹɺҟͳΔεϨου ʹॲཧΛͤΔඞཁ͕͋Δ ɾඳըॲཧͷࡍʹUIεϨουʹॲཧΛ͢ඞཁ͕͋Δ
22 ΞδΣϯμ 1. Coroutineॳ৺ऀʹཱ͔ͪͩΔղͳఆٛͨͪ 2. Androidʹ͓͚ΔThreadͷ෮श 3. CoroutineͱThreadͷؔ 4. Coroutineͷղܾ͢Δ՝
23 CoroutineͱThreadͷؔ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html
24 CoroutineͱThreadͷؔ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html ࣮ߦ͢Δͱ…ʁ A. Start End Hello Stop
B. Start Hello End Stop
25 CoroutineͱThreadͷؔ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࣮ߦ݁Ռ A. Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html
26 CoroutineͱThreadͷؔ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࣮ߦ݁Ռ Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html Coroutine
27 CoroutineͱThreadͷؔ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࣮ߦ݁Ռ Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html 1ඵதஅʂ
Coroutine
28 CoroutineͱThreadͷؔ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࣮ߦ݁Ռ Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html Coroutine
29 CoroutineͱThreadͷؔ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࣮ߦ݁Ռ Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html ࠶։
Coroutine
30 CoroutineͱThreadͷؔ Ͳ͜Ͱಈ͍͍ͯΔͷ͔͔֬ΊΔ ࣮ߦ݁Ռ Start thread is main End thread
is DefaultDispatcher-worker-1 Hello Stop
31 CoroutineͱThreadͷؔ Ͳ͜Ͱಈ͍͍ͯΔͷ͔͔֬ΊΔ ࣮ߦ݁Ռ Start thread is main End thread
is DefaultDispatcher-worker-1 Hello Stop
CoroutineͱThreadͷؔ CoroutineɺThread্Ͱಈ͍͍ͯΔΜͩͶ (ࢸۃͨΓલͳͰ͋Δ)
CoroutineͱThreadͷؔ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action
CoroutineͱThreadͷؔ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action தஅΛࣔ͢म০ࢠ
35 CoroutineͱThreadͷؔ suspendम০ࢠ ࣮ߦ݁Ռ Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html 1ඵதஅʂ
Coroutine
36 CoroutineͱThreadͷؔ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html suspendम০ࢠ தஅΛࣔ͢म০ࢠ
37 CoroutineͱThreadͷؔ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html 1ඵதஅʂ Coroutine
38 CoroutineͱThreadͷؔ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html ଓߦʂ ผεϨουͰ࣮ߦதʂ
39 CoroutineͱThreadͷؔ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html ࠶։
CoroutineͱThreadͷؔ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action தஅΛࣔ͢म০ࢠ
CoroutineͱThreadͷؔ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action
CoroutineͱThreadͷؔ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action ࣮ߦ݁Ռ 1 threads active
at the start 4 threads active at the end Took 1031 ms
CoroutineͱThreadͷؔ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action ࣮ߦ݁Ռ 1 threads active
at the start 4 threads active at the end Took 1031 ms 1ສݸͷίϧʔνϯʹରͯ͠εϨου3ͭ
CoroutineͱThreadͷؔ CoroutineͱThreadͷؔ ͦΕͧΕͷCoroutineThreadʹΞαΠϯ͞ΕΔɻ CoroutineΛ͏ͱɺগͳ͍ThreadͷੜͰฒߦॲཧΛߦ͏͜ͱ͕Մೳ
CoroutineͱThreadͷؔ ͦΕͧΕͷCoroutineThreadʹΞαΠϯ͞ΕΔɻ CoroutineΛ͏ͱɺগͳ͍ThreadͷੜͰฒߦॲཧΛߦ͏͜ͱ͕Մೳ suspendम০ࢠΛ༻͍ͨίϧʔνϯͷதஅͱɺ࣮ߦεϨουͷղ์
46 CoroutineͱThreadͷؔ ͬͪ͜ͷํ͕Θ͔Γ͍͢ʁ Coroutines are light weight threads => Coroutine
is a light way to do concurrency with threads
ࢀߟ threads are to coroutines what CPU cores are to
threads ࢀߟɿhttps//stackoverflow.com/questions/49606471/why-and-when-to-use-co-routines- instead-of-threads-in-android-using-kotlin-as-th Coroutineʹͱͬͯthreadͱɺ threadʹͱͬͯͷCPUͱಉ͡Α͏ͳͷͰ͋Δ
48 ΞδΣϯμ 1. Coroutineॳ৺ऀʹཱ͔ͪͩΔղͳఆٛͨͪ 2. Androidʹ͓͚ΔThreadͷ෮श 3. CoroutineͱThreadͷؔ 4. Coroutineͷղܾ͢Δ՝
Coroutineͷղܾ͢Δ՝ Coroutineɺগͳ͍εϨουͰޮతʹฒߦॲཧΛ͚͞Δʂ
Coroutineͷղܾ͢Δ՝ Coroutineɺগͳ͍εϨουͰޮతʹฒߦॲཧΛ͚͞Δʂ ͦͦAndroidͰͦΜͳʹಉ࣌ʹ࣮ߦ͢Δ͜ͱ͋Δͷ…? ଞʹྑ͍͜ͱͳ͍ͷ…?
Coroutineͷղܾ͢Δ՝ KotlinʹCoroutine͕ಋೖ͞ΕΔલͷ Android։ൃʹ͓͚Δඇಉظॲཧ ɾHandlerThread / AsyncTask ɾRxJava
Coroutineͷղܾ͢Δ՝ HandlerThread / AsyncTask ࢀߟɿhttps://academy.realm.io/jp/posts/android-thread-looper-handler/
Coroutineͷղܾ͢Δ՝ HandlerThread / AsyncTask ɾCallback hell ɾLifecycleʹ߹ΘͤͨϝϞϦཧ
Coroutineͷղܾ͢Δ՝ RxJava
Coroutineͷղܾ͢Δ՝ RxJava ίʔϧόοΫͷଟॏωετϝϞϦཧଟগղܾ͞Εͨ ͕ɺେ͛͞ ͦͦStreamѻ͏ͨΊͷlibrary
Coroutineͷղܾ͢Δ՝ Coroutine
Coroutineͷղܾ͢Δ՝ Coroutine readableͰγϯϓϧͳඇಉظॲཧ ɾno more callback hell ɾno more rxͷཞ༻
ɾஞ࣍ॲཧͰͷهड़
·ͱΊ ɾCoroutine is a light way to concurrency with threads
ɾܰྔͰฒߦॲཧʂ ɾreadableͰγϯϓϧͳฒߦॲཧʂ
·ͱΊ खΛಈ͔ͯ͠ྑ͍KotlinϥΠϑΛʂ https://kotlinlang.org/