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 an army of robots
kneath
303
45k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
175
52k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.2k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
30
4.6k
How GitHub (no longer) Works
holman
314
140k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
40
2k
What's in a price? How to price your products and services
michaelherold
244
12k
A better future with KSS
kneath
238
17k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.5k
Raft: Consensus for Rubyists
vanstee
137
6.8k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
650
Mobile First: as difficult as doing things right
swwweet
223
9.5k
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/