Slide 1

Slide 1 text

৮ͬͯΘ͔ΔCoroutine FiNC Technologies Inc. / Engineer Development Kensuke Izumi

Slide 2

Slide 2 text

ࣗݾ঺հ Kensuke Izumi Android Engineer @FiNC Kotlin / javascript / ruby Twitter: @izmeal2000

Slide 3

Slide 3 text

͍͔ʹͯ͠๻͸reject͞Εͨͷ͔

Slide 4

Slide 4 text

͍͔ʹͯ͠๻͸reject͞Εͨͷ͔ νʔϜ಺Ͱͷෳ਺ճͷϨϏϡʔΛܦͯ ϒϥογϡΞοϓ͞ΕͨCFP!

Slide 5

Slide 5 text

͍͔ʹͯ͠๻͸reject͞Εͨͷ͔ ͗Γ͗Γ·Ͱ೪ͬͯमਖ਼ʹमਖ਼ΛॏͶͯSUBMIT!!! At 23:59 2018/10/21

Slide 6

Slide 6 text

͍͔ʹͯ͠๻͸reject͞Εͨͷ͔

Slide 7

Slide 7 text

͍͔ʹͯ͠๻͸reject͞Εͨͷ͔ Rejected by System

Slide 8

Slide 8 text

ΞδΣϯμ 1. Coroutineॳ৺ऀʹཱͪ͸͔ͩΔ೉ղͳఆٛͨͪ 2. Androidʹ͓͚ΔThreadͷ෮श 3. CoroutineͱThreadͷؔ܎ 4. Coroutineͷղܾ͢Δ՝୊

Slide 9

Slide 9 text

ΞδΣϯμ 1. Coroutineॳ৺ऀʹཱͪ͸͔ͩΔ೉ղͳఆٛͨͪ 2. Androidʹ͓͚ΔThreadͷ෮श 3. CoroutineͱThreadͷؔ܎ 4. Coroutineͷղܾ͢Δ՝୊

Slide 10

Slide 10 text

What is Coroutine?

Slide 11

Slide 11 text

What is Coroutine? Coroutines are light weight threads

Slide 12

Slide 12 text

What is Coroutine? Coroutines are light weight threads ?

Slide 13

Slide 13 text

ු͔ΜͰ͖ͨٙ໰ Coroutines are light weight threads ίϧʔνϯ͸ܰྔεϨου

Slide 14

Slide 14 text

ු͔ΜͰ͖ͨٙ໰ Coroutines are light weight threads CoroutineͬͯThreadͱର౳ʹൺֱͯ͠ྑ͍΋ͷͳͷʁ ݁ہJVMεϨου্Ͱ࣮ߦ͞ΕΔͷͰ͸ʁ

Slide 15

Slide 15 text

ͦ΋ͦ΋Threadͬͯɺɺɺ ๻͕Android։ൃΛ࢝Ίͨ౰࣌ͷঢ়گ ඇಉظॲཧʁɹRXʂ

Slide 16

Slide 16 text

ͦ΋ͦ΋Threadͬͯɺɺɺ ๻͕Android։ൃΛ࢝Ίͨ౰࣌ͷঢ়گ ඇಉظॲཧʁɹRXʂ Thread? observeOnͱ͔Ͱࢦఆ͢Δ΍ͭʂ

Slide 17

Slide 17 text

Threadͷࡶͳೝࣝ Threadͷ෮श͔Β࢝ΊΑ͏

Slide 18

Slide 18 text

ΞδΣϯμ 1. Coroutineॳ৺ऀʹཱͪ͸͔ͩΔ೉ղͳఆٛͨͪ 2. Androidʹ͓͚ΔThreadͷ෮श 3. CoroutineͱThreadͷؔ܎ 4. Coroutineͷղܾ͢Δ՝୊

Slide 19

Slide 19 text

Androidʹ͓͚ΔThreadͷ෮श Androidͷ̐ͭͷίϯϙʔωϯτͱϓϩηε ɾActivity ɾService ɾBroadcastReceiver ɾContent Providers ɹ (جຊతʹ) ΞϓϦ͸ҰͭͷϓϩηεΛىಈ͍ͯͯ͠ɺϚϧνεϨουͰಈ͘

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

21 Androidʹ͓͚ΔThreadͷ෮श Androidʹ͓͚ΔϚϧνεϨουͷ੍໿ ɾωοτϫʔΫॲཧ΍DBͷॻ͖ࠐΈͳͲΛߦ͏ࡍʹ͸ɺҟͳΔεϨου ʹॲཧΛ೚ͤΔඞཁ͕͋Δ ɾඳըॲཧͷࡍʹ͸UIεϨουʹॲཧΛ໭͢ඞཁ͕͋Δ

Slide 22

Slide 22 text

22 ΞδΣϯμ 1. Coroutineॳ৺ऀʹཱͪ͸͔ͩΔ೉ղͳఆٛͨͪ 2. Androidʹ͓͚ΔThreadͷ෮श 3. CoroutineͱThreadͷؔ܎ 4. Coroutineͷղܾ͢Δ՝୊

Slide 23

Slide 23 text

23 CoroutineͱThreadͷؔ܎ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html

Slide 24

Slide 24 text

24 CoroutineͱThreadͷؔ܎ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html ࣮ߦ͢Δͱ…ʁ A. Start End Hello Stop B. Start Hello End Stop

Slide 25

Slide 25 text

25 CoroutineͱThreadͷؔ܎ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࣮ߦ݁Ռ A. Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html

Slide 26

Slide 26 text

26 CoroutineͱThreadͷؔ܎ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࣮ߦ݁Ռ Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html Coroutine

Slide 27

Slide 27 text

27 CoroutineͱThreadͷؔ܎ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࣮ߦ݁Ռ Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html 1ඵதஅʂ Coroutine

Slide 28

Slide 28 text

28 CoroutineͱThreadͷؔ܎ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࣮ߦ݁Ռ Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html Coroutine

Slide 29

Slide 29 text

29 CoroutineͱThreadͷؔ܎ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࣮ߦ݁Ռ Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html ࠶։ Coroutine

Slide 30

Slide 30 text

30 CoroutineͱThreadͷؔ܎ Ͳ͜Ͱಈ͍͍ͯΔͷ͔͔֬ΊΔ ࣮ߦ݁Ռ Start thread is main End thread is DefaultDispatcher-worker-1 Hello Stop

Slide 31

Slide 31 text

31 CoroutineͱThreadͷؔ܎ Ͳ͜Ͱಈ͍͍ͯΔͷ͔͔֬ΊΔ ࣮ߦ݁Ռ Start thread is main End thread is DefaultDispatcher-worker-1 Hello Stop

Slide 32

Slide 32 text

CoroutineͱThreadͷؔ܎ CoroutineɺThread্Ͱಈ͍͍ͯΔΜͩͶ (ࢸۃ౰ͨΓલͳ࿩Ͱ͸͋Δ)

Slide 33

Slide 33 text

CoroutineͱThreadͷؔ܎ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action

Slide 34

Slide 34 text

CoroutineͱThreadͷؔ܎ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action தஅΛࣔ͢म০ࢠ

Slide 35

Slide 35 text

35 CoroutineͱThreadͷؔ܎ suspendम০ࢠ ࣮ߦ݁Ռ Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html 1ඵதஅʂ Coroutine

Slide 36

Slide 36 text

36 CoroutineͱThreadͷؔ܎ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html suspendम০ࢠ தஅΛࣔ͢म০ࢠ

Slide 37

Slide 37 text

37 CoroutineͱThreadͷؔ܎ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html 1ඵதஅʂ Coroutine

Slide 38

Slide 38 text

38 CoroutineͱThreadͷؔ܎ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html ଓߦʂ ผεϨουͰ࣮ߦதʂ

Slide 39

Slide 39 text

39 CoroutineͱThreadͷؔ܎ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html ࠶։

Slide 40

Slide 40 text

CoroutineͱThreadͷؔ܎ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action தஅΛࣔ͢म০ࢠ

Slide 41

Slide 41 text

CoroutineͱThreadͷؔ܎ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action

Slide 42

Slide 42 text

CoroutineͱThreadͷؔ܎ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action ࣮ߦ݁Ռ 1 threads active at the start 4 threads active at the end Took 1031 ms

Slide 43

Slide 43 text

CoroutineͱThreadͷؔ܎ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action ࣮ߦ݁Ռ 1 threads active at the start 4 threads active at the end Took 1031 ms 1ສݸͷίϧʔνϯʹରͯ͠εϨου3ͭ

Slide 44

Slide 44 text

CoroutineͱThreadͷؔ܎ CoroutineͱThreadͷؔ܎ ͦΕͧΕͷCoroutine͸ThreadʹΞαΠϯ͞ΕΔɻ CoroutineΛ࢖͏ͱɺগͳ͍Threadͷੜ੒ͰฒߦॲཧΛߦ͏͜ͱ͕Մೳ

Slide 45

Slide 45 text

CoroutineͱThreadͷؔ܎ ͦΕͧΕͷCoroutine͸ThreadʹΞαΠϯ͞ΕΔɻ CoroutineΛ࢖͏ͱɺগͳ͍Threadͷੜ੒ͰฒߦॲཧΛߦ͏͜ͱ͕Մೳ suspendम০ࢠΛ༻͍ͨίϧʔνϯͷதஅͱɺ࣮ߦεϨουͷղ์

Slide 46

Slide 46 text

46 CoroutineͱThreadͷؔ܎ ͬͪ͜ͷํ͕Θ͔Γ΍͍͢ʁ Coroutines are light weight threads => Coroutine is a light way to do concurrency with threads

Slide 47

Slide 47 text

ࢀߟ 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ͱಉ͡Α͏ͳ΋ͷͰ͋Δ

Slide 48

Slide 48 text

48 ΞδΣϯμ 1. Coroutineॳ৺ऀʹཱͪ͸͔ͩΔ೉ղͳఆٛͨͪ 2. Androidʹ͓͚ΔThreadͷ෮श 3. CoroutineͱThreadͷؔ܎ 4. Coroutineͷղܾ͢Δ՝୊

Slide 49

Slide 49 text

Coroutineͷղܾ͢Δ՝୊ Coroutineɺগͳ͍εϨουͰޮ཰తʹฒߦॲཧΛ͞͹͚Δʂ

Slide 50

Slide 50 text

Coroutineͷղܾ͢Δ՝୊ Coroutineɺগͳ͍εϨουͰޮ཰తʹฒߦॲཧΛ͞͹͚Δʂ ͦ΋ͦ΋AndroidͰͦΜͳʹಉ࣌ʹ࣮ߦ͢Δ͜ͱ͋Δͷ…? ଞʹྑ͍͜ͱͳ͍ͷ…?

Slide 51

Slide 51 text

Coroutineͷղܾ͢Δ՝୊ KotlinʹCoroutine͕ಋೖ͞ΕΔલͷ Android։ൃʹ͓͚Δඇಉظॲཧ ɾHandlerThread / AsyncTask ɾRxJava

Slide 52

Slide 52 text

Coroutineͷղܾ͢Δ՝୊ HandlerThread / AsyncTask ࢀߟɿhttps://academy.realm.io/jp/posts/android-thread-looper-handler/

Slide 53

Slide 53 text

Coroutineͷղܾ͢Δ՝୊ HandlerThread / AsyncTask ɾCallback hell ɾLifecycleʹ߹ΘͤͨϝϞϦ؅ཧ

Slide 54

Slide 54 text

Coroutineͷղܾ͢Δ՝୊ RxJava

Slide 55

Slide 55 text

Coroutineͷղܾ͢Δ՝୊ RxJava ίʔϧόοΫͷଟॏωετ΍ϝϞϦ؅ཧ໰୊͸ଟগղܾ͞Εͨ ͕ɺେ͛͞ ͦ΋ͦ΋Streamѻ͏ͨΊͷlibrary

Slide 56

Slide 56 text

Coroutineͷղܾ͢Δ՝୊ Coroutine

Slide 57

Slide 57 text

Coroutineͷղܾ͢Δ՝୊ Coroutine readableͰγϯϓϧͳඇಉظॲཧ ɾno more callback hell ɾno more rxͷཞ༻ ɾஞ࣍ॲཧͰͷهड़

Slide 58

Slide 58 text

·ͱΊ ɾCoroutine is a light way to concurrency with threads ɾܰྔͰฒߦॲཧʂ ɾreadableͰγϯϓϧͳฒߦॲཧʂ

Slide 59

Slide 59 text

·ͱΊ खΛಈ͔ͯ͠ྑ͍KotlinϥΠϑΛʂ https://kotlinlang.org/