Kotlin Coroutinesで共有リソースに正しくアクセスする
by
Mori Atsushi
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
.PSJ"UTVTIJ ,PUMJO$PSPVUJOFTͰ ڞ༗Ϧιʔεʹਖ਼͘͠ΞΫηε͢Δ ,PUMJO'FTU ,PUMJOݴޠɾϓϩάϥϛϯά
Slide 2
Slide 2 text
LINEϠϑʔגࣜձࣾ / Android 2019 ະ౿εʔύʔΫϦΤʔλ Mori Atsushi X: @at_sushi_at IUUQTHJUIVCDPNNPSJBUTVTIJ
Slide 3
Slide 3 text
No content
Slide 4
Slide 4 text
"
Slide 5
Slide 5 text
$PSPVUJOFͷىಈ
Slide 6
Slide 6 text
Slide 7
Slide 7 text
ࠓճͷΰʔϧ Kotlin CoroutinesΛͬͯɺ ࣗ৴Λ࣋ͬͯڞ༗ϦιʔεΛѻ͑ΔΑ͏ʹ͢Δ 1. ڝ߹ঢ়ଶ͕ൃੜ͢Δཧ༝ͱղܾํ๏ 2. ୯ҰεϨουͷࡍʹؾΛ͚ͭΔ͖ 3. ԋश 🚀
Slide 8
Slide 8 text
ڝ߹ঢ়ଶ͕ൃੜ͢Δཧ༝ͱ ղܾํ๏ 1
Slide 9
Slide 9 text
࠶ܝ
Slide 10
Slide 10 text
ΛಡΈऔͬͯॻ͖ࠐΉ
Slide 11
Slide 11 text
ݪҼɿಉ࣌ʹߋ৽͕Δ J εϨου εϨου UFNQ UFNQ J J
Slide 12
Slide 12 text
ϒϩοΫಉ࣌ʹ͔ͭ͠ॲཧ͞Εͳ͍
Slide 13
Slide 13 text
MutexʹΑΔϩοΫ J εϨου εϨου UFNQ UFNQ J NVUFY ϩοΫ J ϩοΫղআ ϩοΫதͷͨΊػ ϩοΫղআ ϩοΫ
Slide 14
Slide 14 text
૿ͨ͠ͷΛฦؔ͢ εϨου҆શͳ*OUFHFS ѻ͍͍ͨσʔλʹରԠͨ͠λΠϓ͕ଘࡏ͠ͳ͍߹͑ͳ͍ ,PUMJO/BUJWFͰ"UPNJD*OU͕͑Δ
Slide 15
Slide 15 text
ϒϩοΫಉ࣌ʹ͔ͭ͠ॲཧ͞Εͳ͍
Slide 16
Slide 16 text
ؔಉ࣌ʹ͔̍ͭ͠ॲཧ͞Εͳ͍
Slide 17
Slide 17 text
MutexͱSynchronizedͷҧ͍ MutexɿϒϩοΫ͞Ε͍ͯΔؒɺͦͷCoroutineதஅঢ়ଶʹͳΔ SynchronizedɿϒϩοΫ͞Ε͍ͯΔؒɺεϨουΛઐ༗͠ଓ͚Δ CoroutinesΛ͍ͬͯΔ߹ → Mutex suspendͰͳ͍ؔ → Synchronized
Slide 18
Slide 18 text
ෳͷεϨουͰಉ͡ϦιʔεΛ ѻ͏߹ɿ - AtomicIntegerͷεϨου҆શͳ λΠϓΛ͏ - MutexSynchronizedͰϩοΫ͢Δ 💡
Slide 19
Slide 19 text
୯ҰεϨουͷࡍʹ ؾΛ͚ͭΔ͖ 2
Slide 20
Slide 20 text
Dispatcherͷछྨ Dispatchers.Main: UIॲཧ Dispatchers.Default: όοΫάϥϯυॲཧ Dispatchers.IO: ωοτϫʔΫDBΞΫηε
Slide 21
Slide 21 text
Dispatcherͷछྨ Dispatchers.Main: UIॲཧ Dispatchers.Default: όοΫάϥϯυॲཧ Dispatchers.IO: ωοτϫʔΫDBΞΫηε εϨου ෳεϨου ෳεϨου
Slide 22
Slide 22 text
εϨου͔ͩΒڝ߹ঢ়ଶى͖ͳ͍ʁ
Slide 23
Slide 23 text
No content
Slide 24
Slide 24 text
ճݺΕΔ
Slide 25
Slide 25 text
ݪҼɿFetchதʹॲཧ͞ΕΔͨΊ MBTU'FUDIFE"U.JMMJT $PSPVUJOF $PSPVUJOF GFUDI ʜ GFUDI ʜ
Slide 26
Slide 26 text
ͷΈݺΕΔ ϩοΫͯ͠ಉ࣌ʹ࣮ߦ͞Εͳ͍Α͏ʹ͢Δ
Slide 27
Slide 27 text
ͷΈݺΕΔ 4VTQFOEؔͷݺͼग़͠લʹߋ৽͢Δ ϝΠϯεϨου͔Βݺͼग़͢ඞཁ͕͋Δ͜ͱΛ໌ࣔ BOESPJEYBOOPUBUJPO
Slide 28
Slide 28 text
ίϯύΠϧΤϥʔ suspendؔʹSynchronized͑ͳ͍
Slide 29
Slide 29 text
୯ҰεϨουͰಉ͡ϦιʔεΛ ѻ͏߹ɿ - ಡΈࠐΈͱॻ͖ࠐΈͷؒʹsuspend ͕ؔೖΔ߹ʹҙ͕ඞཁ - MutexͰϩοΫՄೳ 💡
Slide 30
Slide 30 text
ԋश 3
Slide 31
Slide 31 text
ෳεϨουͰݺͼग़͍ͨ͠ ԋश̍
Slide 32
Slide 32 text
.VUFYͰϩοΫ͢Δ
Slide 33
Slide 33 text
εϨου҆શͳ .VUBCMF4UBUF'MPXVQEBUF Λ͏
Slide 34
Slide 34 text
GVODUJPOෳճ ݺͼग़͞ΕΔՄೳੑ͕͋Δ functionʹ෭࡞༻Λ͍͚࣋ͨͤͯͳ͍
Slide 35
Slide 35 text
FNJU ෳεϨου͔Β҆શʹݺͼग़ͤΔ ͜͜ಉ͡εϨουʹͳΔ $IBOOFMͰՄ
Slide 36
Slide 36 text
ԋश2 TVTQFOEGVODUJPO
Slide 37
Slide 37 text
.VUFYͰϩοΫ͢Δ
Slide 38
Slide 38 text
.VUFYͰϩοΫ͢Δ ҟͳΔIDͷϢʔβʔΛಉ࣌ʹऔಘͰ͖ͳ͍
Slide 39
Slide 39 text
*%͝ͱʹϩοΫΛ࡞͢Δ *%͝ͱͷϩοΫΛऔಘ͢ΔͨΊʹ ϩοΫ͢Δඞཁ͕͋Δ
Slide 40
Slide 40 text
%FGGFSFEΛ࡞Δ͚ͩͳͷͰTVTQFOEঢ়ଶʹͳΒͳ͍ ΞϓϦέʔγϣϯείʔϓͷ$PSPVUJOF4DPQF 6TFSͷΘΓʹ%FGGFSFEΛอ࣋͢Δ .BQͷૢ࡞ϩοΫதʹߦ͏
Slide 41
Slide 41 text
εϨου҆શͳ.VUBCMF.BQ IUUQTHJUIVCDPNUPVDIMBC4UBUFMZ +7.ͳΒ$PODVSSFOU)BTI.BQ͕͑Δ
Slide 42
Slide 42 text
ඇಉظॲཧ૾Ҏ্ʹෳࡶ - Ͱ͖Δ͚ͩ1εϨουͰѻ͑ΔΑ͏ʹ͢Δ - ෳCoroutine͔ΒΞΫηε͞ΕΔڞ༗ ϦιʔεͰ͖Δ͚ͩݮΒ͢ - ෳͷղܾࡦ͔ΒγϯϓϧͳͷΛબͿ 🚀