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

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

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

takahirom

July 03, 2018
Tweet

More Decks by takahirom

Other Decks in Programming

Transcript

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

    ˖ "CFNB57ך"OESPJE،فٔ׾⡲׏גְתׅկ ˖ 2JJUBח׮剅ְ׋ךדծ׉׸׾鋅זָ׵耀ְג ֻ׌ְׁկ ˖ IUUQTRJJUBDPNUBLBIJSPNJUFNT CBBEDDGCB 荈ⴓחאְג
  2. ⢪׻׸׷$PSPVUJOFTך"1* ˖ MBVODI ˖ BTZOD ˖ BXBJU ˖ XJUI$POUFYU ˖

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

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

    $PSPVUJOF$POUFYU ˖ $PNNPO1PPM ˖ $POUJOVBUJPO ˖ TVTQFOE$PSPVUJOF ˖ $PSPVUJOF%JTQBUDIFS
  5. ⢪׻׸׷$PSPVUJOFTך"1* ˖ MBVODI ˖ BTZOD ˖ BXBJU ˖ XJUI$POUFYU ˖

    $PSPVUJOF$POUFYU ˖ $PNNPO1PPM ˖ $POUJOVBUJPO ˖ TVTQFOE$PSPVUJOF ˖ $PSPVUJOF%JTQBUDIFS ˖ %FGFSSFE
  6. ⢪׻׸׷$PSPVUJOFTך"1* ˖ MBVODI ˖ BTZOD ˖ BXBJU ˖ XJUI$POUFYU ˖

    $P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˖ %FGFSSFE ˖ 6ODPOOFE ˖ BOESPJE6* ˖ %FGBVMU%JTQBUDIFS ˖ 3FOEF[WPVT$IBOOFM ˖ 4FOE$IBOOFM ˖ 3FDFJWF$IBOOFM ˖ BDUPS ˖ SVO#MPDLJOH ˖ KPJO
  15. ⢪׻׸׷$PSPVUJOFTך"1* ˖ MBVODI ˖ BTZOD ˖ BXBJU ˖ XJUI$POUFYU ˖

    $PSPVUJOF$POUFYU ˖ $PNNPO1PPM ˖ $POUJOVBUJPO ˖ TVTQFOE$PSPVUJOF ˖ $PSPVUJOF%JTQBUDIFS ˖ %FGFSSFE ˖ 6ODPOOFE ˖ BOESPJE6* ˖ %FGBVMU%JTQBUDIFS ˖ 3FOEF[WPVT$IBOOFM ˖ 4FOE$IBOOFM ˖ 3FDFJWF$IBOOFM ˖ BDUPS ˖ SVO#MPDLJOH ˖ KPJO ˖ 
  16. 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ָ寸ת׷
  17. 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
  18. 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
  19. MBVODI ד⢪׻׸גְ׷ $PNNPO1PPM object CommonPool : CoroutineDispatcher() { override fun

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

    dispatch( context: CoroutineContext, block: Runnable ) = try { (pool ?: getOrCreatePoolSync()).execute(timeSource.trackTask(block)) } … 5ISFBE1PPMָזֽ׸ל⡲׶ծFYFDVUFׅ׷׌ֽ
  21. 鑐׃ח$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׾竰䪫׃׋ؙٓأ׾⡲׷
  22. 鑐׃ח$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׾䭷㹀ׅ׷
  23. 鑐׃ח$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ًاحس׾㹋鄲׃ג׫׷
  24. 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 ׉ךתתךأٖحسד 㹋遤דֹ׋
  25. ⚥倖 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׾⢪ֲ
  26. 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⚥倖דֹ׷
  27. 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 ׾⢪ֲהⱄꟚדֹ׷
  28. 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
  29. 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׾⡲׷
  30. 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ׅ׷׌ֽ
  31. 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ח״׷㹋鄲
  32. 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׾鵤ׅ
  33. 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ך鸐⥋أٖحسד《䖤׃גծ 《䖤דֹ如痥ⱄꟚׅ׷կ
  34. 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
  35. 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