Upgrade to Pro — share decks privately, control downloads, hide ads and more …

完全に理解した気になる Kotlin Coroutines

完全に理解した気になる Kotlin Coroutines

7166bc2cbc462ab5fd1987a76d0fe709?s=128

takahirom

July 03, 2018
Tweet

Transcript

  1. 㸣Ⰻח椚鍑׃׋孡חז׷ ,PUMJO$PSPVUJOFT UBLBIJSPN

  2. ˖ UBLBIJSPN !OFX@SVOOBCMF דׅկ ˖ 劤せכ嬁「䃨峔 ׭׿ׄױ׋ַמ׹ דׅկ ˖ "OESPJEָ㥨ֹדׅ

    ˖ "CFNB57ך"OESPJE،فٔ׾⡲׏גְתׅկ ˖ 2JJUBח׮剅ְ׋ךדծ׉׸׾鋅זָ׵耀ְג ֻ׌ְׁկ ˖ IUUQTRJJUBDPNUBLBIJSPNJUFNT CBBEDDGCB 荈ⴓחאְג
  3. $PSPVUJOFTך 椚鍑ָꨇ׃ְ

  4. ⢪׻׸׷$PSPVUJOFTך"1*

  5. ⢪׻׸׷$PSPVUJOFTך"1* ˖ MBVODI

  6. ⢪׻׸׷$PSPVUJOFTך"1* ˖ MBVODI ˖ BTZOD

  7. ⢪׻׸׷$PSPVUJOFTך"1* ˖ MBVODI ˖ BTZOD ˖ BXBJU

  8. ⢪׻׸׷$PSPVUJOFTך"1* ˖ MBVODI ˖ BTZOD ˖ BXBJU ˖ XJUI$POUFYU

  9. ⢪׻׸׷$PSPVUJOFTך"1* ˖ MBVODI ˖ BTZOD ˖ BXBJU ˖ XJUI$POUFYU ˖

    $PSPVUJOF$POUFYU
  10. ⢪׻׸׷$PSPVUJOFTך"1* ˖ MBVODI ˖ BTZOD ˖ BXBJU ˖ XJUI$POUFYU ˖

    $PSPVUJOF$POUFYU ˖ $PNNPO1PPM
  11. ⢪׻׸׷$PSPVUJOFTך"1* ˖ MBVODI ˖ BTZOD ˖ BXBJU ˖ XJUI$POUFYU ˖

    $PSPVUJOF$POUFYU ˖ $PNNPO1PPM ˖ $POUJOVBUJPO
  12. ⢪׻׸׷$PSPVUJOFTך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׍ׯ׿ה椚鍑׃׋ְ

  27. MBVODI launch { println("thread:"+Thread.currentThread()) } MBVODIדDPSPUVJOF׾饯⹛ׅ׷

  28. MBVODI launch { println("thread:"+Thread.currentThread()) } thread:Thread[ForkJoinPool.commonPool-worker-2,5,main] MBVODIדDPSPUVJOF׾饯⹛ׅ׷

  29. MBVODI launch { println("thread:"+Thread.currentThread()) } thread:Thread[ForkJoinPool.commonPool-worker-2,5,main] ֿ̓ך5ISFBEכוַֿ׵勻׋ MBVODIדDPSPUVJOF׾饯⹛ׅ׷

  30. MBVODI launch { println("thread:"+Thread.currentThread()) } thread:Thread[ForkJoinPool.commonPool-worker-2,5,main] MBVODIדDPSPUVJOF׾饯⹛ׅ׷ MBVODIך⚥׾鋅ח遤ֲֿ

  31. MBVODI ך⚥魦 public fun launch( context: CoroutineContext = DefaultDispatcher, start:

    CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job { 㹋כ$PSPVUJOF$POUFYUד㹋遤ׅ׷5ISFBEָ寸ת׷
  32. MBVODI ך$PSPVUJOF$POUFYU public fun launch( context: CoroutineContext = DefaultDispatcher, start:

    CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job { 䒷侧ך$PSPVUJOF$POUFYUכرؿٕؓزד%FGBVMU%JTQBUDIFS 植朐כ%FGBVMU%JTQBUDIFSכ$PNNPO1PPM public val DefaultDispatcher: CoroutineDispatcher = CommonPool
  33. MBVODI ך$PSPVUJOF$POUFYU public fun launch( context: CoroutineContext = DefaultDispatcher, start:

    CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job { $PNNPO1PPM public val DefaultDispatcher: CoroutineDispatcher = CommonPool
  34. MBVODI ד⢪׻׸גְ׷ $PNNPO1PPM object CommonPool : CoroutineDispatcher() { override fun

    dispatch( context: CoroutineContext, block: Runnable ) = try { (pool ?: getOrCreatePoolSync()).execute(timeSource.trackTask(block)) } … $PNNPO1PPMך EJTQBUDIًاحسָㄎל׸׷
  35. MBVODI ד⢪׻׸גְ׷ $PNNPO1PPM object CommonPool : CoroutineDispatcher() { override fun

    dispatch( context: CoroutineContext, block: Runnable ) = try { (pool ?: getOrCreatePoolSync()).execute(timeSource.trackTask(block)) } … 5ISFBE1PPMָזֽ׸ל⡲׶ծFYFDVUFׅ׷׌ֽ
  36. 知⽃ז
 %JTQBUDIFS׾⡲׏ג׫׷

  37. 鑐׃ח$PSPVUJOF%JTQBUDIFS׾
 ⡲׏ג׫׷ class MyDispatcher : CoroutineDispatcher() { override fun dispatch(context:

    CoroutineContext, block: Runnable) { println("dispatch:before") block.run() println("dispatch:after") } } launch(MyDispatcher()) { println("thread:"+Thread.currentThread()) } $PSPVUJOF%JTQBUDIFS׾竰䪫׃׋ؙٓأ׾⡲׷
  38. 鑐׃ח$PSPVUJOF%JTQBUDIFS׾
 ⡲׏ג׫׷ class MyDispatcher : CoroutineDispatcher() { override fun dispatch(context:

    CoroutineContext, block: Runnable) { println("dispatch:before") block.run() println("dispatch:after") } } launch(MyDispatcher()) { println("thread:"+Thread.currentThread()) } 荈ⴓד⡲׏׋%JTQBUDIFS׾䭷㹀ׅ׷
  39. 鑐׃ח$PSPVUJOF%JTQBUDIFS׾
 ⡲׏ג׫׷ class MyDispatcher : CoroutineDispatcher() { override fun dispatch(context:

    CoroutineContext, block: Runnable) { println("dispatch:before") block.run() println("dispatch:after") } } launch(MyDispatcher()) { println("thread:"+Thread.currentThread()) } EJTQBUDIًاحس׾㹋鄲׃ג׫׷
  40. class MyDispatcher : CoroutineDispatcher() { override fun dispatch(context: CoroutineContext, block:

    Runnable) { println("dispatch:before") block.run() println("dispatch:after") } } launch(MyDispatcher()) { println("thread:"+Thread.currentThread()) } EJTQBUDICFGPSF UISFBE5ISFBE<NBJO  NBJO> EJTQBUDIBGUFS ׉ךתתךأٖحسד 㹋遤דֹ׋
  41. $PSPVUJOFT׏ג5ISFBEה ずׄדכ

  42. $PSPVUJOFTחכ
 ⚥倖ָ֮׷

  43. ⚥倖 class MyDispatcher : CoroutineDispatcher() { val executor = Executors.newSingleThreadExecutor()

    override fun dispatch(context: CoroutineContext, block: Runnable) { executor.execute { println("dispatch:before") block.run() println("dispatch:after") } } } launch(MyDispatcher()) { println("1:" + Thread.currentThread()) suspendCoroutine<String> { continuation -> thread { // ී௨ʹthreadΛ࡞࣮ͬͯߦ͢Δ // தஅ͞ΕͨCoroutinesΛ࠶։͢Δ continuation.resume("test") } } println("2:" + Thread.currentThread()) } 㹋ꥷך⹛ֹח
 さ׻ׇ׷׋׭ ➙㔐ך⢽דכ FYFDVUPS׾⢪ֲ
  44. class MyDispatcher : CoroutineDispatcher() { val executor = Executors.newSingleThreadExecutor() override

    fun dispatch(context: CoroutineContext, block: Runnable) { executor.execute { println("dispatch:before") block.run() println("dispatch:after") } } } launch(MyDispatcher()) { println("1:" + Thread.currentThread()) suspendCoroutine<String> { continuation -> thread { // ී௨ʹthreadΛ࡞࣮ͬͯߦ͢Δ // தஅ͞ΕͨCoroutinesΛ࠶։͢Δ continuation.resume("test") } } println("2:" + Thread.currentThread()) } TVTQFOE$PSPVUJOF ׾⢪ֲה DPSPVUJOFT⚥倖דֹ׷
  45. class MyDispatcher : CoroutineDispatcher() { val executor = Executors.newSingleThreadExecutor() override

    fun dispatch(context: CoroutineContext, block: Runnable) { executor.execute { println("dispatch:before") block.run() println("dispatch:after") } } } launch(MyDispatcher()) { println("1:" + Thread.currentThread()) suspendCoroutine<String> { continuation -> thread { // ී௨ʹthreadΛ࡞࣮ͬͯߦ͢Δ // தஅ͞ΕͨCoroutinesΛ࠶։͢Δ continuation.resume("test") } } println("2:" + Thread.currentThread()) } SFTVNF ׾⢪ֲהⱄꟚדֹ׷
  46. override fun dispatch(context: CoroutineContext, block: Runnable) executor.execute { println("dispatch:before") block.run()

    println("dispatch:after") } } } launch(MyDispatcher()) { println("1:" + Thread.currentThread()) suspendCoroutine<String> { continuation -> thread { // ී௨ʹthreadΛ࡞࣮ͬͯߦ͢Δ // தஅ͞ΕͨCoroutinesΛ࠶։͢Δ continuation.resume("test") } } println("2:" + Thread.currentThread()) } ⚥倖⵸ה⚥倖䖓ךⱄꟚד 㔐5ISFBE1PPMד 㹋遤ׁ׸גְ׷ EJTQBUDICFGPSF 5ISFBE<QPPMUISFBE  NBJO> EJTQBUDIBGUFS  ͜͜ͰҰ౓தஅ  EJTQBUDICFGPSF 5ISFBE<QPPMUISFBE  NBJO> EJTQBUDIBGUFS
  47. "OESPJEד 㹋ꥷוֲ⢪ֲ

  48. launch(UI) { val response = api.fetch() Snackbar.make(view, "code:${response.code}", Snackbar.LENGTH_LONG) .setAction("Action",

    null).show() } "1*ַ׵《䖤׃גծ׉ךⰻ㺁׾4OBDLCBSח⳿ׅ⢽
  49. launch(UI) { val response = api.fetch() Snackbar.make(view, "code:${response.code}", Snackbar.LENGTH_LONG) .setAction("Action",

    null).show() } "1*ַ׵《䖤׃גծ׉ךⰻ㺁׾4OBDLCBSח⳿ׅ⢽ 6*ծծ
  50. val UI = HandlerContext(Handler(Looper.getMainLooper()), “UI") public class HandlerContext( private val

    handler: Handler, private val name: String? = null ) : CoroutineDispatcher(), Delay { … override fun dispatch(context: CoroutineContext, block: Runnable) { handler.post(block) } launch(UI) { val response = api.fetch() Snackbar.make(view, "code:${response.code}", Snackbar.LENGTH_LONG) .setAction("Action", null).show() } "OESPJEך)BOEMFS׾⡲׷
  51. val UI = HandlerContext(Handler(Looper.getMainLooper()), “UI") public class HandlerContext( private val

    handler: Handler, private val name: String? = null ) : CoroutineDispatcher(), Delay { … override fun dispatch(context: CoroutineContext, block: Runnable) { handler.post(block) } launch(UI) { val response = api.fetch() Snackbar.make(view, "code:${response.code}", Snackbar.LENGTH_LONG) .setAction("Action", null).show() } "OESPJEך)BOEMFSחQPTUׅ׷׌ֽ
  52. launch(UI) { val response = api.fetch() Snackbar.make(view, "code:${response.code}", Snackbar.LENGTH_LONG) .setAction("Action",

    null).show() }
  53. launch(UI) { val response = api.fetch() Snackbar.make(view, "code:${response.code}", Snackbar.LENGTH_LONG) .setAction("Action",

    null).show() } GFUDI ך⚥כ
  54. launch(UI) { val response = api.fetch() Snackbar.make(view, "code:${response.code}", Snackbar.LENGTH_LONG) .setAction("Action",

    null).show() } Api.kt interface ApiSearvice { @GET("/") fun fetchDeferrd(@Query("zipcode") zipCode: String = "1000000"): Deferred<Response> } val service = retrofit.create(ApiSearvice::class.java) suspend fun fetch(): Response { return service.fetchDeferrd().await() } 3FUSPUח״׷㹋鄲
  55. launch(UI) { val response = api.fetch() Snackbar.make(view, "code:${response.code}", Snackbar.LENGTH_LONG) .setAction("Action",

    null).show() } Api.kt interface ApiSearvice { @GET("/") fun fetchDeferrd(@Query("zipcode") zipCode: String = "1000000"): Deferred<Response> } val service = retrofit.create(ApiSearvice::class.java) suspend fun fetch(): Response { return service.fetchDeferrd().await() } SFUSPULPUMJODPSPVUJOFTBEBQUFS׾⢪ֲה
 %FGFSSFE׾鵤ׅ
  56. launch(UI) { val response = api.fetch() Snackbar.make(view, "code:${response.code}", Snackbar.LENGTH_LONG) .setAction("Action",

    null).show() } Api.kt interface ApiSearvice { @GET("/") fun fetchDeferrd(@Query("zipcode") zipCode: String = "1000000"): Deferred<Response> } val service = retrofit.create(ApiSearvice::class.java) suspend fun fetch(): Response { return service.fetchDeferrd().await() } %FGFSSFEכBXBJU ׅ׷ֿהד穠卓׾《䖤דֹ׷ BXBJU ׾ㄎעֿהדDPSPVUJOFTָ⚥倖׃גծ 0L)UUQך鸐⥋أٖحسד《䖤׃גծ 《䖤דֹ如痥ⱄꟚׅ׷կ
  57. launch(UI) { val response = api.fetch() Snackbar.make(view, "code:${response.code}", Snackbar.LENGTH_LONG) .setAction("Action",

    null).show() } Api.kt interface ApiSearvice { @GET("/") fun fetchDeferrd(@Query("zipcode") zipCode: String = "1000000"): Deferred<Response> } val service = retrofit.create(ApiSearvice::class.java) suspend fun fetch(): Response { return service.fetchDeferrd().await() } TVTQFOEًاحس׾⢪ֲהծ BXBJU ׾剅ַ׆חず劍涸ח《䖤׃גְ׷״ֲח剅ֽ׷ ,PUMJOךط؎ذ؍ـזأة؎ٕכֿך剅ֹ倯 ˖ IUUQTHJUIVCDPN,PUMJOLPUMJODPSPVUJOFTCMPCNBTUFSLPUMJODPSPVUJOFTJOGPSNBMNEBTZODISPOPVTQSPHSBNNJOHTUZMFT
  58. launch(UI) { val response = api.fetch() Snackbar.make(view, "code:${response.code}", Snackbar.LENGTH_LONG) .setAction("Action",

    null).show() } Api.kt interface ApiSearvice { @GET("/") fun fetchDeferrd(@Query("zipcode") zipCode: String = "1000000"): Deferred<Response> } val service = retrofit.create(ApiSearvice::class.java) suspend fun fetch(): Response { return service.fetchDeferrd().await() } TVTQFOEًاحس׾⢪ֲ׋׭ח׻ׂ׻ׂٓحف׃גְ׷ ˖ IUUQTHJUIVCDPN,PUMJOLPUMJODPSPVUJOFTCMPCNBTUFSLPUMJODPSPVUJOFTJOGPSNBMNEBTZODISPOPVTQSPHSBNNJOHTUZMFT
  59. ׌ְ׋ְ椚鍑דֹ׋

  60. תה׭ ˖ $PSPVUJFOTכ5ISFBE♳ד⹛ֻծ⹛ֹכ然钠דֹ׷ ˖ ⚥倖ָדֹגⱄꟚָדֹ׷ ˖ TVTQFOE׾⢪׏׋剅ֹ倯ד剅ְגְֻ