Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Kotlin의 코루틴은 어떻게 동작하는가

7.706 Aufrufe

Veröffentlicht am

* DroidKnights 2018
* Kotlin coroutine에 대한 101 세션 자료

Veröffentlicht in: Technologie
  • Dating direct: ❶❶❶ http://bit.ly/36cXjBY ❶❶❶
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier
  • Dating for everyone is here: ♥♥♥ http://bit.ly/36cXjBY ♥♥♥
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier
  • If you want to download or read this book, copy link or url below in the New tab ......................................................................................................................... DOWNLOAD FULL PDF EBOOK here { https://urlzs.com/UABbn } .........................................................................................................................
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier
  • If you want to download or read this book, copy link or url below in the New tab ......................................................................................................................... DOWNLOAD FULL PDF EBOOK here { https://urlzs.com/UABbn } .........................................................................................................................
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier
  • If you want to download or read this book, copy link or url below in the New tab ......................................................................................................................... DOWNLOAD FULL PDF EBOOK here { https://urlzs.com/UABbn } .........................................................................................................................
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier

Kotlin의 코루틴은 어떻게 동작하는가

  1. 1. Speaker. Kotlin ? 101
  2. 2. fun fetchUserDetail(id: String) { val token = auth() val user = getUser(token, id) updateUserData(user) } Waaaaa~~it 😵 😵
  3. 3. fun fetchUserDetail(id: String) { auth { token -> getUser(token) { updateUserData(it) } } }
  4. 4. suspend fun fetchUserDetail(id: String) { val token = auth() val user = getUser(token, id) updateUserData(user) } suspend
  5. 5. fun plusOne(initial: Int) : Int { val ONE = 1 var result = initial result += ONE return result }
  6. 6. public interface Continuation<in T> { } public val context: CoroutineContext public fun resume(value: T) public fun resumeWithException(exception: Throwable)
  7. 7. suspend fun fetchUserDetail(id: String) { val token = auth() val user = getUser(token, id) updateUserData(user) } }
  8. 8. suspend fun fetchUserDetail(id: String) { // ... switch (label) { case 0: val token = auth() case 1: val user = getUser(token, id) case 2: updateUserData(user) } }
  9. 9. fun fetchUserDetail(id: String) { // ... switch (label) { case 0: val token = auth() case 1: val user = getUser(token, id) case 2: updateUserData(user) } }
  10. 10. fun fetchUserDetail(id: String, cont: Continuation) { switch ( label) { case 0: auth( ) case 1: getUser(token, id ) case 2: … } } val sm = object : CoroutineImpl { ... } sm. sm , sm
  11. 11. fun fetchUserDetail(id: String, cont: Contiunation) { val sm = object : CoroutineImpl { ... } switch (sm.label) { case 0: auth(sm) case 1: getUser(token, id, sm) case 2: … } }
  12. 12. fun fetchUserDetail(id: String, cont: Contiunation) { val sm = object : CoroutineImpl { ... } switch (sm.label) { case 0: sm.id = id sm.label = 1 auth(sm) … } }
  13. 13. fun fetchUserDetail(id: String, cont: Contiunation) { val sm = object : CoroutineImpl { ... } switch (sm.label) { … case 1: val id = sm.id val token = sm.result as String sm.label = 2 getUser(token, id, sm) … }
  14. 14. fun fetchUserDetail(id: String, cont: Contiunation) { val sm = object : CoroutineImpl { fun resume(...) { fetchUserDetail(null, this) } } switch (sm.label) { case 0: … } }
  15. 15. fun fetchUserDetail(id: String, cont: Contiunation) { val sm = cont as? ThisSM ?: object : CoroutineImpl { fun resume(...) { fetchUserDetail(null, this) } } switch (sm.label) { case 0: … } }
  16. 16. public interface Continuation<in T> { } public val context: CoroutineContext public fun resume(value: T) public fun resumeWithException(exception: Throwable)
  17. 17. fun updateUserDetail(id: String) = launch(UI) { val token = auth() val user = getUser(token, id) updateUserDataOntoUI(user) }
  18. 18. class DispatchedContinuation<in T> ( val dispatcher: CoroutineDispatcher, val continuation: Continuation<T> ) : Continuation<T> by continuation { override fun resume(value: T) { dispatcher.dispatch(context, DispatchTask(...)) } ... }
  19. 19. fun launchOnUiThread() { launch(UI) { delay(1000L) ... } } /** * Dispatches execution onto Android main UI thread * and provides native [delay][Delay.delay] support. */ val UI = HandlerContext(Handler(Looper.getMainLooper()), "UI")
  20. 20. fun main(args: Array<String>) = runBlocking<Unit> { val jobs = arrayListOf<Job>() jobs += launch(Unconfined) { println("'Unconfined': I'm working in thread ${Thread.currentThread().name}") } jobs += launch(coroutineContext) { println("'coroutineContext': I'm working in thread ${Thread.currentThread().name}") } jobs += launch(CommonPool) { println("'CommonPool': I'm working in thread ${Thread.currentThread().name}") } jobs += launch(newSingleThreadContext("MyOwnThread")) { println("'newSTC': I'm working in thread ${Thread.currentThread().name}") } jobs.forEach { it.join() } }
  21. 21. fun main(args: Array<String>) = runBlocking<Unit> { launch { repeat(1000) { i -> println("I'm sleeping $i ...") delay(500L) } } delay(1300L) // just quit after delay } I'm sleeping 0 ... I'm sleeping 1 ... I'm sleeping 2 ...
  22. 22. fun main(args: Array<String>) = runBlocking<Unit> { val job = launch { try { repeat(1000) { i -> println("I'm sleeping $i ...") delay(500L) } } finally { println("I'm running finally") } } delay(1300L) // delay a bit println("main: I'm tired of waiting!") job.cancelAndJoin() // cancels the job and waits for its completion println("main: Now I can quit.") } I'm sleeping 0 ... I'm sleeping 1 ... I'm sleeping 2 ... main: I'm tired of waiting! I'm running finally main: Now I can quit.
  23. 23. fun main(args: Array<String>) = runBlocking<Unit> { withTimeout(1300L) { repeat(1000) { i -> println("I'm sleeping $i ...") delay(500L) } } } I'm sleeping 0 ... I'm sleeping 1 ... I'm sleeping 2 ... Exception in thread "main" kotlinx.{…}.TimeoutCancellationException …
  24. 24. https://goo.gl/2Tyx4B

×