Kotlin コルーチンを 理解しよう

800912a73ce4e879003b6c89cf80cfeb?s=47 yagi
August 25, 2018

Kotlin コルーチンを 理解しよう

- コルーチンとはなにか
- Kotlinはどのようにコルーチンを実現しているのか
- Kotlin コルーチンの基本的な使い方

800912a73ce4e879003b6c89cf80cfeb?s=128

yagi

August 25, 2018
Tweet

Transcript

  1. ©2018 Lang-8 Inc. ALL Rights Reserved. ,PUMJO'FTU 5PTIJIJSP:BHJ ,PUMJOίϧʔνϯΛ ཧղ͠Α͏

  2. ©2018 Lang-8 Inc. ALL Rights Reserved. wീ໦ढ़޿ w!TZTZBHJ w$50PG w"OESPJE&OHJOFFS

    "CPVU.F Jetpack Handbook ʲC94৽ץʳ ʮୈ6ষ WorkManagerͱόοΫάϥ΢ϯυλεΫʯ ॻ͖·ͨ͠
  3. ©2018 Lang-8 Inc. ALL Rights Reserved. ©2018 Lang-8 Inc. ALL

    Rights Reserved. https://hinative.com 8FBSFIJSJOH
  4. ©2018 Lang-8 Inc. ALL Rights Reserved. wίϧʔνϯͱ͸ͳʹ͔ w,PUMJO͸ͲͷΑ͏ʹίϧʔνϯΛ࣮ݱ ͍ͯ͠Δͷ͔ w,PUMJOίϧʔνϯͷجຊతͳ࢖͍ํ

    ࠓ೔࿩͢͜ͱ
  5. ©2018 Lang-8 Inc. ALL Rights Reserved. wίϧʔνϯͱ͸ͳʹ͔ w,PUMJO͸ͲͷΑ͏ʹίϧʔνϯΛ࣮ݱ ͍ͯ͠Δͷ͔ w,PUMJOίϧʔνϯͷجຊతͳ࢖͍ํ

    ࠓ೔࿩͢͜ͱ
  6. ©2018 Lang-8 Inc. ALL Rights Reserved. wίϧʔνϯͱ͸ͳʹ͔ w,PUMJO͸ͲͷΑ͏ʹίϧʔνϯΛ࣮ݱ ͍ͯ͠Δͷ͔ w,PUMJOίϧʔνϯͷجຊతͳ࢖͍ํ

    ࠓ೔࿩͢͜ͱ
  7. ©2018 Lang-8 Inc. ALL Rights Reserved. wίϧʔνϯͱ͸ͳʹ͔ w,PUMJO͸ͲͷΑ͏ʹίϧʔνϯΛ࣮ݱ ͍ͯ͠Δͷ͔ w,PUMJOίϧʔνϯͷجຊతͳ࢖͍ํ

    ࠓ೔࿩͢͜ͱ
  8. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯͱ͸ͳʹ͔

  9. ©2018 Lang-8 Inc. ALL Rights Reserved. w $PCPMίϯύΠϥΛܰྔʹ࣮ ૷͢ΔͨΊʹίϧʔνϯͷ֓೦ Λಋೖͨ͠

    w ͋Δ৚݅ԼͰϓϩάϥϜΛ෼཭ ͢Δͱಠཱͯͦ͠ΕͧΕಈ࡞Ͱ ͖Δͱ͍͏ΞΠσΞ w DPSPVUJOFɺͭ·Γର౳ͷϧʔ νϯͱ͍͏ҙຯɻ͓ޓ͍Λݺͼ ग़੍ͯ͠ޚͰ͖Δؔ܎ ϝϧϰΟϯɾίϯ΢ΣΠ ͷ೥ͷ࿦จ͕ॳग़ IUUQNFMDPOXBZDPN)PNFQEGDPNQJMFSQEG
  10. ©2018 Lang-8 Inc. ALL Rights Reserved. "GUFSZFBSTʜ

  11. ©2018 Lang-8 Inc. ALL Rights Reserved. w "DPSPVUJOFŠJTBOJOTUBODFPGTVTQFOEBCMF DPNQVUBUJPO*UJTDPODFQUVBMMZTJNJMBSUPB UISFBE

    JOUIFTFOTFUIBUJUUBLFTBCMPDLPG DPEFUPSVOBOEIBTBTJNJMBSMJGFDZDMFŠJU JTDSFBUFEBOETUBSUFE CVUJUJTOPUCPVOEUP BOZQBSUJDVMBSUISFBE*UNBZTVTQFOEJUT FYFDVUJPOJOPOFUISFBEBOESFTVNFJOBOPUIFS POF.PSFPWFS MJLFBGVUVSFPSQSPNJTF JU NBZDPNQMFUFXJUITPNFSFTVMUPSFYDFQUJPO $PSPVUJOFTGPS,PUMJOʹΑΔͱʜ IUUQTHJUIVCDPN,PUMJOLPUMJODPSPVUJOFTCMPCNBTUFSLPUMJODPSPVUJOFTJOGPSNBMNEUFSNJOPMPHZ
  12. ©2018 Lang-8 Inc. ALL Rights Reserved. w ίϧʔνϯ͸ɺҰ࣌ఀࢭՄೳͳܭࢉͷΠϯελϯεͰ͢ɻͦ Ε͸֓೦తʹ͸εϨουʹࣅ͍ͯ·͢ɻͭ·Γɺ࣮ߦ͢Δίʔ υϒϩοΫΛ࣋ͪɺಉ༷ͷϥΠϑαΠΫϧΛ࣋ͪɺ࡞੒͞Ε

    ىಈ͞Ε·͕͢ɺಛఆͷεϨουʹଋറ͞Ε͍ͯ·ͤΜɻ͋ ΔεϨουͰ࣮ߦΛதஅ͠ɺผͷεϨουͰ࠶։͢Δ͜ͱ͕ ͋Γ·͢ɻ͞Βʹɺ'VUVSF΍1SPNJTTͷΑ͏ʹɺԿΒ͔ ͷ݁Ռ΍ྫ֎͕͋Δ৔߹͕͋Γ·͢ɻ $PSPVUJOFTGPS,PUMJOʹΑΔͱʜ IUUQTHJUIVCDPN,PUMJOLPUMJODPSPVUJOFTCMPCNBTUFSLPUMJODPSPVUJOFTJOGPSNBMNEUFSNJOPMPHZ
  13. ©2018 Lang-8 Inc. ALL Rights Reserved. w ίϧʔνϯ͸ɺҰ࣌ఀࢭՄೳͳܭࢉͷΠϯελϯεͰ͢ɻͦ Ε͸֓೦తʹ͸εϨουʹࣅ͍ͯ·͢ɻͭ·Γɺ࣮ߦ͢Δίʔ υϒϩοΫΛ࣋ͪɺಉ༷ͷϥΠϑαΠΫϧΛ࣋ͪɺ࡞੒͞Ε

    ىಈ͞Ε·͕͢ɺಛఆͷεϨουʹଋറ͞Ε͍ͯ·ͤΜɻ͋ ΔεϨουͰ࣮ߦΛதஅ͠ɺผͷεϨουͰ࠶։͢Δ͜ͱ͕ ͋Γ·͢ɻ͞Βʹɺ'VUVSF΍1SPNJTTͷΑ͏ʹɺԿΒ͔ ͷ݁Ռ΍ྫ֎͕͋Δ৔߹͕͋Γ·͢ɻ $PSPVUJOFTGPS,PUMJOʹΑΔͱʜ IUUQTHJUIVCDPN,PUMJOLPUMJODPSPVUJOFTCMPCNBTUFSLPUMJODPSPVUJOFTJOGPSNBMNEUFSNJOPMPHZ
  14. ©2018 Lang-8 Inc. ALL Rights Reserved. wαϒϧʔνϯͱҟͳΓɺঢ়ଶ؅ཧΛ ҙࣝͤͣʹߦ͑ΔͨΊɺڠௐతॲཧɺ ΠςϨʔλɺແݶϦετɺύΠϓͳ Ͳɺܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰

    қʹهड़Ͱ͖Δɻ 8JLJQFEJBʹΑΔͱʜ IUUQTKBXJLJQFEJBPSHXJLJ&#&"#&#$&&#
  15. ©2018 Lang-8 Inc. ALL Rights Reserved. wαϒϧʔνϯͱҟͳΓɺঢ়ଶ؅ཧΛ ҙࣝͤͣʹߦ͑ΔͨΊɺڠௐతॲཧɺ ΠςϨʔλɺແݶϦετɺύΠϓͳ Ͳɺܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰

    қʹهड़Ͱ͖Δɻ 8JLJQFEJBʹΑΔͱʜ IUUQTKBXJLJQFEJBPSHXJLJ&#&"#&#$&&#
  16. ©2018 Lang-8 Inc. ALL Rights Reserved. wҰ࣌ఀࢭՄೳͳܭࢉͷΠϯελϯε wεϨουͷΑ͏ʹ࣮ߦ͢ΔίʔυϒϩοΫΛ࣋ͪɺಉ ༷ͷϥΠϑαΠΫϧΛ࣋ͪɺ࡞੒͞Εىಈ͞ΕΔ͕ɺ ಛఆͷεϨουʹଋറ͞Εͳ͍

    w'VUVSF΍1SPNJTTͷΑ͏ʹɺԿΒ͔ͷ݁Ռ΍ྫ֎ ͕͋Δ৔߹͕͋Δ wܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰қʹهड़Ͱ͖Δ ίϧʔνϯͱ͸ʜ
  17. ©2018 Lang-8 Inc. ALL Rights Reserved. Ұ࣌ఀࢭՄೳͳܭࢉͷΠϯελϯε fun loadProfile(id: Int)

    { val profile = getProfile(id) showProfile(profile) } αϒϧʔνϯ ίϧʔνϯ ։͔࢝ΒϦλʔϯ·Ͱ͕Ұ ͭͷॲཧ୯Ґ தஅ ࠶։ ॲཧͷ్தͰҰ࣌ఀࢭ ͕Մೳ launch { val profile = getProfile(id).await() showProfile(profile) }
  18. ©2018 Lang-8 Inc. ALL Rights Reserved. εϨουͱίϧʔνϯ Thread { val

    profile = getProfile(token) showProfile(profile) }.start() εϨου ίϧʔνϯ ίʔυϒϩοΫͱϥΠϑα ΠΫϧΛ࣋ͪɺ࡞੒ͱىಈ ͕͞ΕΔ εϨουʹࣅ͍ͯΔ͕ ಛఆͷεϨουʹଋറ ͞Εͳ͍ launch { val profile = getProfile(token).await() showProfile(profile) } ͲͷεϨουͰ࣮ߦ͞ΕΔ͔͸ίϧʔνϯࣗ਎͸ؾʹ͠ͳ͍
  19. ©2018 Lang-8 Inc. ALL Rights Reserved. 'VUVSF΍1SPNJTTͷΑ͏ʹ஋Λฦ͢৔߹͕͋Δ fun getProfile(id: Int)

    = async { //… return profile } launch { val profile = getProfile(id).await() showProfile(profile) }
  20. ©2018 Lang-8 Inc. ALL Rights Reserved. 'VUVSF΍1SPNJTTͷΑ͏ʹ஋Λฦ͢৔߹͕͋Δ ஋Λฦ͢ίϧʔνϯ fun getProfile(id:

    Int) = async { //… return profile } launch { val profile = getProfile(id).await() showProfile(profile) }
  21. ©2018 Lang-8 Inc. ALL Rights Reserved. 'VUVSF΍1SPNJTTͷΑ͏ʹ஋Λฦ͢৔߹͕͋Δ fun getProfile(id: Int)

    = async { //… return profile } launch { val profile = getProfile(id).await() showProfile(profile) } ผͷίϧʔνϯΛىಈͯࣗ͠਎͸தஅ ͠ɺ݁ՌΛड͚औͬͯ࠶։͢Δ
  22. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕ ༰қʹهड़Ͱ͖Δ

  23. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸ fun loadProfile(id: Int)

    { val profile = getProfile(id) showProfile(profile) }
  24. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸ ஞ࣮࣍ߦ fun loadProfile(id:

    Int) { val profile = getProfile(id) showProfile(profile) }
  25. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸ ௨৴ॲཧ΍%#ΞΫηεɺॏ͍ͨܭࢉͳͲΛ ߦ͏৔߹ɺϒϩοΩϯάͯ͠͠·͏ɻ fun

    loadProfile(id: Int) { val profile = getProfile(id) showProfile(profile) }
  26. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸ fun getProfile(id: Int,

    f: (Profile) -> Unit) fun loadProfile(id: Int) { val profile = getProfile(id) showProfile(profile) } ίʔϧόοΫΛड͚औΔܗʹͯ͠ʜ
  27. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸ fun getProfile(id: Int,

    f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProgile(profile) } } ϒϩοΩϯά͠ͳ͍ܗʹ͢Δ
  28. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸ fun getProfile(id: Int,

    f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProgile(profile) } }
  29. ©2018 Lang-8 Inc. ALL Rights Reserved. お分かりいただけただろうか・・・

  30. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸ fun getProfile(id: Int,

    f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProgile(profile) } }
  31. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸ fun getProfile(id: Int,

    f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProgile(profile) } } ؔ਺͸͜͜ͰҰ౓தஅ͍ͯ͠Δʂ
  32. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸ fun getProfile(id: Int,

    f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProgile(profile) } } ࠶։͍ͯ͠Δʜ
  33. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸ fun getProfile(id: Int,

    f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProgile(profile) } } ܧଓঢ়گΛ࣋ͭϓϩάϥϜͬ
  34. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ༰қʹॻ͘ fun loadProfile(id: Int)

    { val profile = getProfile(id) showProfile(profile) }
  35. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ༰қʹॻ͘ fun loadProfile(id: Int)

    { val profile = getProfile(id).await() showProfile(profile) } ˞ٖࣅίʔυͰ͢
  36. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ༰қʹॻ͘ fun loadProfile(id: Int)

    { val profile = getProfile(id).await() showProfile(profile) } ˞ٖࣅίʔυͰ͢ HFU1SPpMFؔ਺͕ฦ͢ίϧʔν ϯʹ੍ޚΛ౉ࣗ͠਎͸தஅ͢Δ
  37. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ༰қʹॻ͘ fun loadProfile(id: Int)

    { val profile = getProfile(id).await() showProfile(profile) } ˞ٖࣅίʔυͰ͢ HFU1SPpMFؔ਺͕ฦ͢ίϧʔν ϯ͸ผͷεϨουͰಈ࡞͍ͯ͠ Δ͔΋͠Εͳ͍ͯ͠͠ͳ͍͔΋ ͠Εͳ͍
  38. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ༰қʹॻ͘ fun loadProfile(id: Int)

    { val profile = getProfile(id).await() showProfile(profile) } ˞ٖࣅίʔυͰ͢ HFU1SPpMFؔ਺͕׬ྃͨ͠Β஋ Λड͚औΔͱ͜Ζ͔Β࠶։
  39. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ༰қʹॻ͘ ίϧʔνϯ BTZODBXBJU fun

    loadProfile(id: Int) { getProfile(token) { profile -> showProgile(profile) } } ίʔϧόοΫελΠϧ fun loadProfile(id: Int) { val profile = getProfile(id).await() showProfile(profile) } ˞ٖࣅίʔυͰ͢
  40. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گ͕૿͑ͯ΋ fun loadProfile(id: Int)

    { getProfile(id) { profile -> getReport(profile.id) { report -> //.. } } } } } } ίʔϧόοΫελΠϧ
  41. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گ͕૿͑ͯ΋ ίϧʔνϯ BTZODBXBJU fun

    loadProfile(id: Int) { val profile = getProfile(id).await() val report = getReport(profile.id).await() val a = getA().await() val b = getB().await() val c = getC().await() //… } ˞ٖࣅίʔυͰ͢
  42. ©2018 Lang-8 Inc. ALL Rights Reserved. wδΣωϨʔλɺετϦʔϜ wνϟϯωϧ wΞΫλʔϞσϧ wύΠϓϥΠϯ

    wFUDʜ ܧଓঢ়گΛ؆୯ʹॻ͚Δ͜ͱͰ ࣮૷ָ͕ʹͳΔ΋ͷͨͪ
  43. ©2018 Lang-8 Inc. ALL Rights Reserved. wҰ࣌ఀࢭՄೳͳܭࢉͷΠϯελϯε wεϨουͷΑ͏ʹ࣮ߦ͢ΔίʔυϒϩοΫΛ࣋ͪɺಉ ༷ͷϥΠϑαΠΫϧΛ࣋ͪɺ࡞੒͞Εىಈ͞ΕΔ͕ɺ ಛఆͷεϨουʹଋറ͞Εͳ͍

    w'VUVSF΍1SPNJTTͷΑ͏ʹɺԿΒ͔ͷ݁Ռ΍ྫ֎ ͕͋Δ৔߹͕͋Δ wܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰қʹهड़Ͱ͖Δ ίϧʔνϯͱ͸ʜ
  44. ©2018 Lang-8 Inc. ALL Rights Reserved. ,PUMJO͸ͲͷΑ͏ʹ ίϧʔνϯΛ࣮ݱ͍ͯ͠Δͷ͔ʁ

  45. ©2018 Lang-8 Inc. ALL Rights Reserved. ,PUMJO͸ͲͷΑ͏ʹ ίϧʔνϯΛ࣮ݱ͍ͯ͠Δͷ͔ʁ fun loadProfile(id:

    Int) { val profile = getProfile(id).await() showProfile(profile) } )PX
  46. ©2018 Lang-8 Inc. ALL Rights Reserved. ,PUMJO͸+7.ݴޠ

  47. ©2018 Lang-8 Inc. ALL Rights Reserved. ,PUMJOίϯύΠϥ͸ +BWBόΠτίʔυΛग़ྗ͢ Δ

  48. ©2018 Lang-8 Inc. ALL Rights Reserved. ͭ·Γ,PUMJOͷίϧʔνϯ ͸+BWBͰ࣮૷Ͱ͖Δܗʹ ͳ͍ͬͯΔ͸ͣ

  49. ©2018 Lang-8 Inc. ALL Rights Reserved. ,PUMJO͸ͲͷΑ͏ʹ ίϧʔνϯΛ+BWBͰ࣮ݱͯ͠ ͍Δͷ͔ʁ

  50. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯΛ εςʔτϚγϯʹม׵͍ͯ͠Δ IUUQTHJUIVCDPN,PUMJOLPUMJODPSPVUJOFTCMPCNBTUFSLPUMJODPSPVUJOFTJOGPSNBMNE

  51. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯΛεςʔτϚγϯʹ͢Δ fun simpleCoroutine() {

    val start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") }
  52. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯΛεςʔτϚγϯʹ͢Δ fun simpleCoroutine() {

    val start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ͜͜Ͱதஅ͢Δͱ͢Δ
  53. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯΛεςʔτϚγϯʹ͢Δ fun simpleCoroutine() {

    val start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ̎ͭͷঢ়ଶʹ෼ղͰ͖Δ ̎ͭͷঢ়ଶʹ෼ղͰ͖Δ
  54. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯΛεςʔτϚγϯʹ͢Δ fun simpleCoroutine() {

    val start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ̎ͭͷঢ়ଶʹ෼ղͰ͖Δ ԣஅతʹར༻͢Δม਺
  55. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯΛεςʔτϚγϯʹ͢Δ class SimpleCoroutine {

    var label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTimeMillis() println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.currentTimeMillis() - start}”) label++ } } } }
  56. ©2018 Lang-8 Inc. ALL Rights Reserved. ass SimpleCoroutine { var

    label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTi println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.cu label++ } } }
  57. ©2018 Lang-8 Inc. ALL Rights Reserved. ass SimpleCoroutine { var

    label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTi println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.cu label++ } } } ̎ͭͷঢ়ଶΛ࣋ͭ ̎ͭͷঢ়ଶΛ࣋ͭ
  58. ©2018 Lang-8 Inc. ALL Rights Reserved. ݱࡏͷঢ়ଶΛ࣋ͭ ݱࡏͷঢ়ଶΛද͢ ass SimpleCoroutine

    { var label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTi println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.cu label++ } } }
  59. ©2018 Lang-8 Inc. ALL Rights Reserved. ass SimpleCoroutine { var

    label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTi println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.cu label++ } } } ݱࡏΛมԽͤ͞Δ
  60. ©2018 Lang-8 Inc. ALL Rights Reserved. ڞ௨ͯ͠࢖͏ม਺ ass SimpleCoroutine {

    var label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTi println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.cu label++ } } }
  61. ©2018 Lang-8 Inc. ALL Rights Reserved. ॲཧΛ࠶։͢Δؔ਺ ass SimpleCoroutine {

    var label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTi println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.cu label++ } } }
  62. ©2018 Lang-8 Inc. ALL Rights Reserved. ass SimpleCoroutine { var

    label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTi println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.cu label++ } } }
  63. ©2018 Lang-8 Inc. ALL Rights Reserved. fun delay(delayTime: Long, coroutine:

    SimpleCoroutine) { Thread { Thread.sleep(delayTime) coroutine.resume() }.start() }
  64. ©2018 Lang-8 Inc. ALL Rights Reserved. fun delay(delayTime: Long, coroutine:

    SimpleCoroutine) { Thread { Thread.sleep(delayTime) coroutine.resume() }.start() } ࠶։ͷͨΊʹ4JNQMF$PSPVUJOF Λड͚औ͓ͬͯ͘
  65. ©2018 Lang-8 Inc. ALL Rights Reserved. ࢦఆ͞Εͨ࣌ؒεϦʔϓͯ͠ 4JNQMF$PSPVUJOFΛ࠶։͢Δ fun delay(delayTime:

    Long, coroutine: SimpleCoroutine) { Thread { Thread.sleep(delayTime) coroutine.resume() }.start() }
  66. ©2018 Lang-8 Inc. ALL Rights Reserved. ass SimpleCoroutine { var

    label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTi println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.cu label++ } } }
  67. ©2018 Lang-8 Inc. ALL Rights Reserved. val simpleCoroutine = SimpleCoroutine()

    simpleCoroutine.resume() fun simpleCoroutine() { val start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ίϧʔνϯ εςʔτϚγϯ
  68. ©2018 Lang-8 Inc. ALL Rights Reserved. val simpleCoroutine = SimpleCoroutine()

    simpleCoroutine.resume() fun simpleCoroutine() { val start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ίϧʔνϯ εςʔτϚγϯ TUBSU FOE TUBSU FOE
  69. ©2018 Lang-8 Inc. ALL Rights Reserved. εςʔτϚγϯͰίϧʔνϯΛ࣮ݱ͢Δ wதஅͱ࠶։Λঢ়ଶભҠͱݟཱͯΔ w࠶։ʹඞཁͳ৘ใΛεςʔτϚγϯ಺ʹ Ωϟϓνϟ͢Δ

    w಺෦ঢ়ଶΛมԽͤ͞ͳ͕Β࣮ߦ͢Δ
  70. ©2018 Lang-8 Inc. ALL Rights Reserved. Ͳ͏΍ͬͯίϧʔνϯΛ εςʔτϚγϯʹม׵͢Δͷ͔

  71. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEम০ࢠͱ ܧଓ

  72. ©2018 Lang-8 Inc. ALL Rights Reserved. wίϧʔνϯΛϚʔΩϯά͢ΔͨΊʹTVTQFOEम ০ࢠΛಋೖ wϥϜμࣜʹTVTQFOEम০ࢠΛ෇༩͢Δͱίϧʔ νϯຊମΛද͢

    TVTQFOEϥϜμ  wؔ਺ʹ෇༩͢Δͱதஅ͢Δ৔ॴΛද͢ɻͦͷؔ਺Λ TVTQFOEؔ਺ͱݺͿ wTVTQFOEؔ਺͸TVTQFOEؔ਺ϥϜμ͔Β͔͠ ݺͼग़ͤͳ͍ TVTQFOEम০ࢠ
  73. ©2018 Lang-8 Inc. ALL Rights Reserved. wίϧʔνϯࣗ਎ΛܧଓΦϒδΣΫτʹ͢Δ wTVTQFOEؔ਺Λܧଓ౉͠ελΠϧ $POUJOVBUJPO1BTTJOH4UZMF $14

    ʹม׵͢Δ wTVTQFOEؔ਺಺ͰܧଓΦϒδΣΫτΛऔΓ ग़ͯ͠೚ҙʹ࠶։Ͱ͖ΔΑ͏ʹ͢Δ ܧଓ
  74. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔ਺ͱ$14 fun delay(delayTime: Long,

    coroutine: SimpleCoroutine) { Thread { Thread.sleep(delayTime) coroutine.resume() }.start() }
  75. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔ਺ͱ$14 suspend fun delay(delayTime:

    Long) { Thread { Thread.sleep(delayTime) coroutine.resume() }.start() }
  76. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔ਺ͱ$14 suspend fun delay(delayTime:

    Long) { Thread { Thread.sleep(delayTime) coroutine.resume() }.start() } // c: Continuation<T> ίϯύΠϧ࣌ʹࣗಈͰ ܧଓΠϯλϑΣʔεͷ Ҿ਺͕૿͑Δ
  77. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔ਺ͱ$14 suspend fun delay(delayTime:

    Long) { // c: Continuation<T> suspendCoroutine<Unit> { c -> Thread { Thread.sleep(delayTime) c.resume(Unit) }.start() } }
  78. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔ਺ͱ$14 suspend fun delay(delayTime:

    Long) { // c: Continuation<T> suspendCoroutine<Unit> { c -> Thread { Thread.sleep(delayTime) c.resume(Unit) }.start() } } ܧଓΠϯλϑΣʔεΛऔΓग़͢ ͨΊʹ༻ҙ͞Ε͍ͯΔؔ਺
  79. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔ਺ͱ$14 suspend fun delay(delayTime:

    Long) { // c: Continuation<T> suspendCoroutine<Unit> { c -> Thread { Thread.sleep(delayTime) c.resume(Unit) }.start() } } ܧଓΠϯλϑΣʔεΛ࢖ͬͯ࠶։ ॲཧΛߦ͏
  80. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {

    val start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") }
  81. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {

    val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } }
  82. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {

    val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } } TVTQFOEϥϜμʹΑͬͯίϧʔνϯͷ ൣғ͕ܾ·Δɻ
  83. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {

    val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } f.startCoroutine(NoOpCompletion) }
  84. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {

    val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } f.startCoroutine(NoOpCompletion) } TVTQFOEϥϜμͷ֦ுؔ਺͕͍͔ͭ͘༻ ҙ͞Ε͍ͯΔɻTUBSU$PSPVUJOFؔ਺ʹΑͬ ͯίϧʔνϯΛ։࢝Ͱ͖Δ
  85. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {

    val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } f.startCoroutine(NoOpCompletion) } TUBSU$PSPVUJOFؔ਺͸$POUJOVBUJPO ΛҾ਺ʹऔΔɻίϧʔνϯ͕׬ྃͨ͠Βݺ ͼग़͞ΕΔ
  86. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {

    val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } f.startCoroutine(NoOpCompletion) } ৑௕ʹͳΔͨΊศ্ٓ༻ҙ ͨ͠Ϋϥε object NoOpCompletion<T> : Continuation<T> { override val context: CoroutineContext = EmptyCoroutineContext override fun resume(value: T) { // no op } override fun resumeWithException(exception: Throwable) { // no op }
  87. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {

    val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } f.startCoroutine(NoOpCompletion) }
  88. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {

    val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } f.startCoroutine(NoOpCompletion) } TUBSU FOE
  89. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯϏϧμʔͱ ܧଓΠϯλʔηϓλʔ

  90. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯΛ࡞Δͷ͸ΊΜͲ͍͘͞ fun simpleCoroutine() {

    val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } f.startCoroutine(NoOpCompletion) }
  91. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯϏϧμʔ fun simpleCoroutine() {

    launch { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } } ͦΕͧΕͷੑ࣭Λ΋ͭίϧʔνϯϏϧμʔ ͕ίϧʔνϯඪ४ϥΠϒϥϦͰఏڙ͞ΕΔ ͷͰීஈ͸ͦͪΒΛ࢖͏͜ͱʹͳΔɻ
  92. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓΠϯλʔηϓλʔͱ ࣮ߦεϨου launch(CommonPool) {

    // … } launch(UI) { // … } launch(JavaFx) { // … } launch(Swing) { // … } async(CommonPool) { // … }
  93. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓΠϯλʔηϓλʔͱ ࣮ߦεϨου launch(CommonPool) {

    // … } launch(UI) { // … } launch(JavaFx) { // … } launch(Swing) { // … } async(CommonPool) { // … } ίϧʔνϯϏϧμʔʹܧଓΠϯ λʔηϓλʔΛ౉࣮ͯ͠ߦεϨο υΛίϯτϩʔϧͰ͖Δ
  94. ©2018 Lang-8 Inc. ALL Rights Reserved. wίϧʔνϯΛεςʔτϚγϯʹม׵͠ ͍ͯΔ wม׵ͷͨΊʹTVTQFOEम০ࢠͱܧ ଓΛಋೖͨ͠

    wܧଓΠϯλʔηϓλʔʹΑΓɺ࣮ߦͷ εϨουΛݶఆ͠ͳ͍ ,PUMJO͸ͲͷΑ͏ʹ ίϧʔνϯΛ+BWBͰ࣮ݱ͍ͯ͠Δͷ͔ʁ
  95. ©2018 Lang-8 Inc. ALL Rights Reserved. ,PUMJOίϧʔνϯͷ جຊతͳ࢖͍ํ

  96. ©2018 Lang-8 Inc. ALL Rights Reserved. ,PUMJO͔Βొ৔

  97. ©2018 Lang-8 Inc. ALL Rights Reserved. ֤छػೳ͸ϥΠϒϥϦͰఏڙ wDPSF SFBDUJWF OBUJWF

    VJ KTͳͲ ͷϞδϡʔϧʹ෼͔Ε͍ͯΔ IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFT
  98. ©2018 Lang-8 Inc. ALL Rights Reserved. ಛఆͷ؀ڥ޲͚ͷϥΠϒϥϦ΋͋Δ w"OESPJE +BWB'Y 4XJOH޲͚͕༻ҙ͞

    Ε͍ͯΔ ܧଓΠϯλʔηϓλʔͷ࣮૷ IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFTUSFFNBTUFSVJ
  99. ©2018 Lang-8 Inc. ALL Rights Reserved. wLPUMJOYDPSPVUJOFTFYQFSJNFOUBMύοέʔ δʹؔ࿈͢ΔΫϥε͕͋Δ wຊ൪౤ೖʹ͸ͦΕͳΓʹ֮ޛ͕ඞཁ͕ͩͬͨ FYQFSJNFOUBM࿮Ͱఏڙ

  100. ©2018 Lang-8 Inc. ALL Rights Reserved. ͰTUBCMFʹͳΔ༧ఆʂʂ

  101. ©2018 Lang-8 Inc. ALL Rights Reserved.

  102. ©2018 Lang-8 Inc. ALL Rights Reserved. ˞ࠓճ͸·ͩFYQFSJNFOUBMͷ؀ڥͷํͰղઆ͠·͢

  103. ©2018 Lang-8 Inc. ALL Rights Reserved. ؀ڥߏங kotlin { experimental

    { coroutines 'enable' } } dependencies { implementation”org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.60" implementation “org.jetbrains.kotlinx:kotlinx-coroutines-core:0.24.0" // platformʹ߹Θͤͯ௥Ճ implementation “org.jetbrains.kotlinx:kotlinx-coroutines-android:0.24.0" } BQQCVJMEHSBEMF
  104. ©2018 Lang-8 Inc. ALL Rights Reserved. ؀ڥߏங kotlin { experimental

    { coroutines 'enable' } } dependencies { implementation”org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.60" implementation “org.jetbrains.kotlinx:kotlinx-coroutines-core:0.24.0" // platformʹ߹Θͤͯ௥Ճ implementation “org.jetbrains.kotlinx:kotlinx-coroutines-android:0.24.0" } BQQCVJMEHSBEMF ແͯ͘΋ಈ͚͘Ͳ*%&্ͱ ͔Ͱܯࠂ͕ͰΔ
  105. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSFϥΠϒϥϦ͕ఏڙ͢ΔίϧʔνϯϏϧμʔ

  106. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSFϥΠϒϥϦ͕ఏڙ͢ΔίϧʔνϯϏϧμʔ

  107. ©2018 Lang-8 Inc. ALL Rights Reserved. ࠷ॳͷίϧʔνϯ launch { val

    start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") }
  108. ©2018 Lang-8 Inc. ALL Rights Reserved. ࠷ॳͷίϧʔνϯ launch { val

    start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ίϧʔνϯϏϧμʔ
  109. ©2018 Lang-8 Inc. ALL Rights Reserved. w݁ՌΛ࣋ͨͳ͍ίϧʔνϯΛ࡞੒͢Δί ϧʔνϯϏϧμʔؔ਺ MBVODIؔ਺ public

    fun launch( context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job
  110. ©2018 Lang-8 Inc. ALL Rights Reserved. MBVODIؔ਺ public fun launch(

    context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job ίϧʔνϯຊମɻ$PSPVUJOF4DPQFͷ֦ு TVTQFOEϥϜμ
  111. ©2018 Lang-8 Inc. ALL Rights Reserved. MBVODIؔ਺ public fun launch(

    context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job ίϧʔνϯͷ࣮ߦίϯςΩετɺڞ༗σʔ λ΍ܧଓΠϯλʔηϓλʔͳͲΛ͜͜Ͱࢦ ఆͰ͖Δ
  112. ©2018 Lang-8 Inc. ALL Rights Reserved. MBVODIؔ਺ public fun launch(

    context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job
  113. ©2018 Lang-8 Inc. ALL Rights Reserved. %FGBVMU%JTQBUDIFS public actual val

    DefaultDispatcher: CoroutineDispatcher = if (useCoroutinesScheduler) ExperimentalCoroutineDispatcher() else CommonPool
  114. ©2018 Lang-8 Inc. ALL Rights Reserved. %FGBVMU%JTQBUDIFS tcher() else CommonPool

  115. ©2018 Lang-8 Inc. ALL Rights Reserved. %FGBVMU%JTQBUDIFS tcher() else CommonPool

    εϨουϓʔϧΛ࢖ͬͯίϧʔνϯΛ࣮ߦ͢Δ ܧଓΠϯλʔηϓλʔɻ࣮ߦ؀ڥͷ$16ίΞ ਺ʹԠͯ͡εϨου਺͸มԽ͢Δ
  116. ©2018 Lang-8 Inc. ALL Rights Reserved. MBVODIؔ਺ public fun launch(

    context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job ίϧʔνϯͷ։࢝ํ๏Λࢦఆ͢ΔɻσϑΥ ϧτͰ͸ଈ࠲ʹ։࢝͢Δ
  117. ©2018 Lang-8 Inc. ALL Rights Reserved. MBVODIؔ਺ public fun launch(

    context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job ىಈͨ͠ίϧʔνϯͷ׬ྃΛ଴ͪ߹Θͤͨ ΓΩϟϯηϧͰ͖Δ
  118. ©2018 Lang-8 Inc. ALL Rights Reserved. MBVODIؔ਺ public fun launch(

    context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job ਌+PCΛઃఆ͢Δɻෳ਺ͷίϧʔνϯ ΛҰׅͰDBODFM͍ͨ࣌͠ͳͲʹ࢖͏
  119. ©2018 Lang-8 Inc. ALL Rights Reserved. MBVODIؔ਺ public fun launch(

    context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job ίϧʔνϯͷ׬ྃΛίʔϧόοΫͰड͚औ Γ͍ͨ৔߹ʹࢦఆ͢Δ
  120. ©2018 Lang-8 Inc. ALL Rights Reserved. ࠷ॳͷίϧʔνϯ launch { val

    start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") }
  121. ©2018 Lang-8 Inc. ALL Rights Reserved. ࠷ॳͷίϧʔνϯ launch { val

    start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ඪ४ϥΠϒϥϦ͕ఏڙ͢Δ TVTQFOEؔ਺
  122. ©2018 Lang-8 Inc. ALL Rights Reserved. ࠷ॳͷίϧʔνϯ launch { val

    start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } TUBSU FOE
  123. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯͱ࣮ߦεϨου launch { val

    start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ͲͪΒͷঢ়ଶ΋εϨουϓʔ ϧͰ࣮ߦ͞ΕΔ
  124. ©2018 Lang-8 Inc. ALL Rights Reserved. ྫ͑͹6*Λ৮ΔΑ͏ͳίϧʔνϯͩͱʜ launch { prograssBar.isVisible

    = true delay(1000) prograssBar.isVisible = false }
  125. ©2018 Lang-8 Inc. ALL Rights Reserved. ྫ͑͹6*Λ৮ΔΑ͏ͳίϧʔνϯͩͱʜ launch { prograssBar.isVisible

    = true delay(1000) prograssBar.isVisible = false } %FGBVMU%JTQBUDIFS͸εϨουϓʔ ϧͰίϧʔνϯΛ࣮ߦ͢Δ
  126. ©2018 Lang-8 Inc. ALL Rights Reserved. ྫ͑͹6*Λ৮ΔΑ͏ͳίϧʔνϯͩͱʜ launch(UI) { prograssBar.isVisible

    = true delay(1000) prograssBar.isVisible = false }
  127. ©2018 Lang-8 Inc. ALL Rights Reserved. ྫ͑͹6*Λ৮ΔΑ͏ͳίϧʔνϯͩͱʜ launch(UI) { prograssBar.isVisible

    = true delay(1000) prograssBar.isVisible = false } LPUMJOYDPSPVUJOFTBOESPJE͕ఏڙ͢Δ ܧଓΠϯλʔηϓλʔɻৗʹ6*εϨουͰ࠶։ ͢Δ
  128. ©2018 Lang-8 Inc. ALL Rights Reserved. ྫ͑͹6*Λ৮ΔΑ͏ͳίϧʔνϯͩͱʜ launch(UI) { prograssBar.isVisible

    = true delay(1000) prograssBar.isVisible = false } 6*εϨουͰ࣮ߦ 6*εϨουͰ࣮ߦ
  129. ©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU launch(UI) { prograssBar.isVisible

    = true delay(1000) prograssBar.isVisible = false }
  130. ©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:

    String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true delay(1000) prograssBar.isVisible = false }
  131. ©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:

    String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true delay(1000) prograssBar.isVisible = false } ٖࣅతͳ௨৴ॲཧ
  132. ©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:

    String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true val profile = loadProfile() nameText.text = profile.name prograssBar.isVisible = false }
  133. ©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:

    String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true val profile = loadProfile() nameText.text = profile.name prograssBar.isVisible = false } ϒϩοΩϯά͢Δ
  134. ©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:

    String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } BTZODؔ਺Λ࢖͏
  135. ©2018 Lang-8 Inc. ALL Rights Reserved. w݁ՌΛ࣋ͭίϧʔνϯΛ࡞੒͢Δίϧʔ νϯϏϧμʔؔ਺ BTZODؔ਺ public

    fun <T> async( context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> T ): Deferred<T>
  136. ©2018 Lang-8 Inc. ALL Rights Reserved. %FGFSSFE5 public interface Deferred<out

    T> : Job { //… public suspend fun await(): T //… } தஅͯ݁͠ՌΛ଴ͬͯ࠶։͢Δ
  137. ©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:

    String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false }
  138. ©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:

    String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false }
  139. ©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:

    String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } ܧଓΛड͚औͬͯॲཧͷ׬ྃΛ଴ͭ
  140. ©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:

    String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } ඇಉظॲཧ͕׬ྃ͢Δͱɺ6*εϨο υͰܧଓΛ࠶։͢Δ
  141. ©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:

    String = "Jack") fun loadProfile(): Profile { val profile = http.get(“http:/.”) return profile } launch(UI) { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false }
  142. ©2018 Lang-8 Inc. ALL Rights Reserved. wMBVODIؔ਺͸݁Ռ͕ແ͍ίϧʔνϯΛɺBTZOD ؔ਺͸݁Ռ͕͋ΔίϧʔνϯΛ࡞Δ wσϑΥϧτͰ͸$PNNPO1PPM͕࢖ΘΕΔͷͰ ඞཁʹԠͯ͡ܧଓΠϯλʔηϓλʔΛηοτ͢

    Δ wMBVODIؔ਺ͱBTZODؔ਺Λ૊Έ߹Θͤͯ࢖͏ େମ͜ΕͰࣄ଍ΓΔ ίϧʔνϯͷجຊతͳ࢖͍ํ
  143. ©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJUͱඇಉظॲཧ

  144. ©2018 Lang-8 Inc. ALL Rights Reserved. جຊܗ launch(UI) { prograssBar.isVisible

    = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false }
  145. ©2018 Lang-8 Inc. ALL Rights Reserved. ΤϥʔϋϯυϦϯά launch(UI) { ɹtry{

    prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: Exception) { showError(e) } }
  146. ©2018 Lang-8 Inc. ALL Rights Reserved. ϦΫΤετΛ௚ྻͰ࣮ߦ͢Δ launch(UI) { ɹtry{

    prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: Exception) { showError(e) } }
  147. ©2018 Lang-8 Inc. ALL Rights Reserved. ϦΫΤετΛ௚ྻͰ࣮ߦ͢Δ launch(UI) { ɹtry{

    prograssBar.isVisible = true val token = async { getToken() } val profile = async { loadProfile(token.await()) }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: Exception) { showError(e) } }
  148. ©2018 Lang-8 Inc. ALL Rights Reserved. ϦΫΤετΛ௚ྻͰ࣮ߦ͢Δ launch(UI) { ɹtry{

    prograssBar.isVisible = true val token = async { getToken() } val profile = async { loadProfile(token.await()) }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: Exception) { showError(e) } } ͜͜Ͱ͸தஅͤͣ։࢝ ͚͍ͩͯ͠Δ
  149. ©2018 Lang-8 Inc. ALL Rights Reserved. ϦΫΤετΛ௚ྻͰ࣮ߦ͢Δ launch(UI) { ɹtry{

    prograssBar.isVisible = true val token = async { getToken() } val profile = async { loadProfile(token.await()) }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: Exception) { showError(e) } } ͜͜Ͱ݁ՌΛ଴͍ͬͯΔ
  150. ©2018 Lang-8 Inc. ALL Rights Reserved. ϦΫΤετΛฒྻͰ࣮ߦ͢Δ launch(UI) { ɹtry{

    prograssBar.isVisible = true val profile = async { loadProfile() }.await() val articles = async { loadArticles() }.await() show(profile, articles) prograssBar.isVisible = false } catch (e: Exception) { showError(e) } }
  151. ©2018 Lang-8 Inc. ALL Rights Reserved. ϦΫΤετΛฒྻͰ࣮ߦ͢Δ launch(UI) { ɹtry{

    prograssBar.isVisible = true val profile = async { loadProfile() }.await() val articles = async { loadArticles() }.await() show(profile, articles) prograssBar.isVisible = false } catch (e: Exception) { showError(e) } } ௚ྻͰ࣮ߦ͞Εͯ͠·͏
  152. ©2018 Lang-8 Inc. ALL Rights Reserved. ϦΫΤετΛฒྻͰ࣮ߦ͢Δ launch(UI) { ɹtry{

    prograssBar.isVisible = true val profile = async { loadProfile() } val articles = async { loadArticles() } show(profile.await(), articles.await()) prograssBar.isVisible = false } catch (e: Exception) { showError(e) } }
  153. ©2018 Lang-8 Inc. ALL Rights Reserved. ϦΫΤετΛฒྻͰ࣮ߦ͢Δ launch(UI) { ɹtry{

    prograssBar.isVisible = true val profile = async { loadProfile() } val articles = async { loadArticles() } show(profile.await(), articles.await()) prograssBar.isVisible = false } catch (e: Exception) { showError(e) } } ฒྻͰ։͓͍࢝ͯͯ͠ɺ଴ͪ߹Θͤ Δͱ͜ΖͰҰؾʹBXBJU͢Δ
  154. ©2018 Lang-8 Inc. ALL Rights Reserved. $BODFM launch(UI) { ɹtry{

    prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: Exception) { showError(e) } }
  155. ©2018 Lang-8 Inc. ALL Rights Reserved. $BODFM val job =

    launch(UI) { ɹtry { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: CancellationException) { // cancel } catch (e: Exception) { showError(e) } } //… job.cancel()
  156. ©2018 Lang-8 Inc. ALL Rights Reserved. $BODFM val job =

    launch(UI) { ɹtry { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: CancellationException) { // cancel } catch (e: Exception) { showError(e) } } //… job.cancel() ίϧʔνϯΛىಈ͢Δͱ͖ʹ+PCΛ ड͚औ͓͚ͬͯ͹೚ҙͷλΠϛϯά ͰΩϟϯηϧ͕Ͱ͖Δ
  157. ©2018 Lang-8 Inc. ALL Rights Reserved. $BODFM val job =

    launch(UI) { ɹtry { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: CancellationException) { // cancel } catch (e: Exception) { showError(e) } } //… job.cancel() Ωϟϯηϧͷྫ֎͕ඈΜͰ͘ΔͷͰ Ωϟον
  158. ©2018 Lang-8 Inc. ALL Rights Reserved. $BODFM "OESPJEͷྫ class MainActivity

    : AppCompatActivity() { var job: Job? = null //… fun doSomething(){ job = launch(UI) { … } //… override fun onPause() { job?.cancel() super.onPause() } }
  159. ©2018 Lang-8 Inc. ALL Rights Reserved. $BODFM "OESPJEͷྫ class MainActivity

    : AppCompatActivity() { var job: Job? = null //… fun doSomething(){ job = launch(UI) { … } //… override fun onPause() { job?.cancel() super.onPause() } } ϥΠϑαΠΫϧʹ߹Θͤ ͯΩϟϯηϧ
  160. ©2018 Lang-8 Inc. ALL Rights Reserved. ·ͱΊͯ$BODFM class MainActivity :

    AppCompatActivity() { var rootJob: Job? = null //… fun doSomething(){ launch(UI, parent = rootJob) { … } fun doSomething2(){ launch(UI, parent = rootJob) { … } //… override fun onResume() { super.onResume() rootJob = Job() } override fun onPause() { rootJob.cancel() super.onPause() }
  161. ©2018 Lang-8 Inc. ALL Rights Reserved. ·ͱΊͯ$BODFM class MainActivity :

    AppCompatActivity() { var rootJob: Job? = null override fun onResume() { super.onResume() rootJob = Job() } //… fun doSomething(){ launch(UI, parent = rootJob) { … } fun doSomething2(){ launch(UI, parent = rootJob) { … } //… override fun onPause() { rootJob.cancel() super.onPause() }
  162. ©2018 Lang-8 Inc. ALL Rights Reserved. ·ͱΊͯ$BODFM class MainActivity :

    AppCompatActivity() { var rootJob: Job? = null override fun onResume() { super.onResume() rootJob = Job() } //… fun doSomething(){ launch(UI, parent = rootJob) { … } fun doSomething2(){ launch(UI, parent = rootJob) { … } //… override fun onPause() { rootJob.cancel() super.onPause() }
  163. ©2018 Lang-8 Inc. ALL Rights Reserved. ·ͱΊͯ$BODFM class MainActivity :

    AppCompatActivity() { var rootJob: Job? = null override fun onResume() { super.onResume() rootJob = Job() } //… fun doSomething(){ launch(UI, parent = rootJob) { … } fun doSomething2(){ launch(UI, parent = rootJob) { … } //… override fun onPause() { rootJob?.cancel() super.onPause() } ·ͱΊͯΩϟϯηϧ
  164. ©2018 Lang-8 Inc. ALL Rights Reserved. ϦτϥΠ launch(UI) { try

    { progressBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name progressBar.isVisible = false } catch (e: Exception) { showError(e) } }
  165. ©2018 Lang-8 Inc. ALL Rights Reserved. ϦτϥΠ launch(UI) { repeat(3)

    { try { progressBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name progressBar.isVisible = false return@launch } catch (e: Exception) { if (!shouldRetry(e)) { return@repeat } } } showError() }
  166. ©2018 Lang-8 Inc. ALL Rights Reserved. ϦτϥΠ launch(UI) { repeat(3)

    { try { progressBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name progressBar.isVisible = false return@launch } catch (e: Exception) { if (!shouldRetry(e)) { return@repeat } } } showError() } GPSจͱҰॹɻ͜Ε͸ί ϧʔνϯͰ͸ͳ͍ɻ
  167. ©2018 Lang-8 Inc. ALL Rights Reserved. ϦτϥΠ launch(UI) { repeat(3)

    { try { progressBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name progressBar.isVisible = false return@launch } catch (e: Exception) { if (!shouldRetry(e)) { return@repeat } } } showError() } ੒ޭͨ͠ΒMBVODI͔Β ൈ͚Δ
  168. ©2018 Lang-8 Inc. ALL Rights Reserved. ϦτϥΠ launch(UI) { repeat(3)

    { try { progressBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name progressBar.isVisible = false return@launch } catch (e: Exception) { if (!shouldRetry(e)) { return@repeat } } } showError() } ϦτϥΠ͢΂͖ΤϥʔͰͳ ͍ͳΒSFQFBUΛൈ͚Δ
  169. ©2018 Lang-8 Inc. ALL Rights Reserved. ϦτϥΠ launch(UI) { repeat(3)

    { try { progressBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name progressBar.isVisible = false return@launch } catch (e: Exception) { if (!shouldRetry(e)) { return@repeat } } } showError() } ϦτϥΠ͕શࣦͯഊ͔ͨ͠Ϧτϥ Π͢΂͖Ͱͳ͍Τϥʔ͕ൃੜ৔߹
  170. ©2018 Lang-8 Inc. ALL Rights Reserved. wUSZDBUDIͰΤϥʔॲཧ w௚ྻɺฒྻ͸ݺͼग़͠ํͰίϯτϩʔϧ w+PCΛ࢖ͬͯΩϟϯηϧ wϦτϥΠͳͲ΋ϧʔϓͰॲཧͰ͖Δ

    BTZODBXBJUͱඇಉظॲཧ
  171. ©2018 Lang-8 Inc. ALL Rights Reserved. ·ͩ·ͩ͋ΔΑίϧʔνϯ

  172. ©2018 Lang-8 Inc. ALL Rights Reserved. 3FUSPpUΛ%FGFSSFEԽ IUUQTHJUIVCDPN+BLF8IBSUPOSFUSPpULPUMJODPSPVUJOFTBEBQUFS +BLF8IBSUPO੡ɻ$BMM"EBQUFSΛ࣮૷ͯ͠%FGFSSFE Λฦ͢Α͏ʹ͍ͯ͠Δɻ

    @POST("/api/v1/articles/{id}/like") fun like(@Path(“id") id: Long): Deferred<ResponseBody> @DELETE(“/api/v1/articles/{id}/like") fun likeCancel(@Path("id") id: Long): Deferred<ResponseBody> @POST(“/api/v1/users/sign_up”) fun signUp(@Body params: SignUpParameter): Deferred<SignInResponseEntity> @POST("/api/v1/users/sign_in") fun signIn(@Body params: SignInParameter): Deferred<SignInResponseEntity>
  173. ©2018 Lang-8 Inc. ALL Rights Reserved. &WFOU#VTͱ$IBOOFM class EventBus {

    private val channel = BroadcastChannel<Any>(1) fun send(event: Any, context: CoroutineContext = DefaultDispatcher) { launch(context) { channel.send(event) } } fun subscribe(): ReceiveChannel<Any> = channel.openSubscription() inline fun <reified T> subscribeToEvent() = subscribe().filter { it is T }.map { it as T } } IUUQTHJTUHJUIVCDPNTWFOKBDPCTBCEEBCDEB
  174. ©2018 Lang-8 Inc. ALL Rights Reserved. &WFOU#VTͱ$IBOOFM val bus: EventBus

    by inject() override fun onResume() { super.onResume() launch(UI, parent = rootJob) { bus.subscribeToEvent<LogoutEvent>().consumeEach { // logged out } } } override fun onPause() { rootJob.cancel() super.onPause() } //… bus.send(LogoutEvent)
  175. ©2018 Lang-8 Inc. ALL Rights Reserved. PO"DUJWJUZ3FTVMUΛTVTQFOE launch(UI) { val

    result = activityResult(intent) if (result.isOk) { // ok val data: Intent? = result.flatMap() } else { // ng } } IUUQTHJUIVCDPNQEWSJF[FBOESPJEDPSPVUJOFT BOESPJEDPSPVUJOFTͱ͍͏αʔυύʔςΟϥΠϒϥϦ͕ ఏڙɻSFRVFTU1FSNJTTJPO΋͋ΔΑ
  176. ©2018 Lang-8 Inc. ALL Rights Reserved. w ܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰қʹهड़Ͱ͖ͯศར w ,PUMJO͸ίϧʔνϯΛεςʔτϚγϯʹม׵࣮ͯ͠ݱ͠

    ͍ͯΔ w BTZODBXBJUͳͲͷ֤ػೳ͸,PUMJOͷެࣜϥΠϒϥϦ ͕ఏڙ͍ͯ͠Δ w MBVODIؔ਺ͱBTZODؔ਺Ͱେ఍଍ΓΔ w ඞཁʹԠͯࣗ͡෼Ͱίϧʔνϯͷ࢓૊ΈΛ࣮૷Ͱ͖Δɻ͢ ͰʹαʔυύʔςΟϥΠϒϥϦ͕͍͔ͭ͋͘Δ ,PUMJOίϧʔνϯ·ͱΊ
  177. ©2018 Lang-8 Inc. ALL Rights Reserved. ©2018 Lang-8 Inc. ALL

    Rights Reserved. &OKPZ,PUMJO$PSPVUJOF
  178. ©2018 Lang-8 Inc. ALL Rights Reserved. ©2018 Lang-8 Inc. ALL

    Rights Reserved. ͋Γ͕ͱ͏͍͟͝·ͨ͠