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
610
触って分かるコルーチン
izmeal2000
February 13, 2019
Tweet
Share
More Decks by izmeal2000
See All by izmeal2000
実践、BFF ~ BFFはFiNCのアプリで何を解決したのか ~
kensukeizumi
6
5.2k
Androidの歩数取得とバックグラウンド制限
kensukeizumi
0
4.2k
Featured
See All Featured
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7.1k
GraphQLとの向き合い方2022年版
quramy
44
14k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
Building a Scalable Design System with Sketch
lauravandoore
461
33k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Typedesign – Prime Four
hannesfritz
40
2.5k
Scaling GitHub
holman
459
140k
Measuring & Analyzing Core Web Vitals
bluesmoon
6
260
Why Our Code Smells
bkeepers
PRO
336
57k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
570
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
46
2.3k
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/