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
590
触って分かるコルーチン
izmeal2000
February 13, 2019
Tweet
Share
More Decks by izmeal2000
See All by izmeal2000
実践、BFF ~ BFFはFiNCのアプリで何を解決したのか ~
kensukeizumi
5
5.1k
Androidの歩数取得とバックグラウンド制限
kensukeizumi
0
4.1k
Featured
See All Featured
For a Future-Friendly Web
brad_frost
175
9.4k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
43
2.2k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
830
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
A Modern Web Designer's Workflow
chriscoyier
693
190k
Building Adaptive Systems
keathley
38
2.3k
Agile that works and the tools we love
rasmusluckow
327
21k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
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/