Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Building Kotlin Coroutines Framework from Scrat...

Avatar for Omkar T Omkar T
August 19, 2023

Building Kotlin Coroutines Framework from Scratch by Omkar Tenkale

In this session, we will create the coroutine framework from scratch using only Kotlin's built-in constructs. Our implementation will closely emulate the official version, while keeping things simple enough for all developers to understand. By the end of the session, attendees will have a solid grasp of coroutines and how to use them effectively in their code

For live presentation:
https://omkar-tenkale.github.io/Building-Kotlin-Coroutines-Framework-From-Scratch-Presentation-BLRKotlin/

Avatar for Omkar T

Omkar T

August 19, 2023
Tweet

Other Decks in Programming

Transcript

  1. @SinceKotlin("1.3") public expect fun <T> (suspend () -> T).createCoroutineUnintercepted( completion:

    Continuation<T> ): Continuation<Unit> 1 2 3 4 5 @SinceKotlin("1.3") 6 public expect fun <R, T> (suspend R.() -> T).createCoroutineUnintercepted( 7 receiver: R, 8 completion: Continuation<T> 9 ): Continuation<Unit> 10
  2. val completionCallback = object : Continuation<Unit> { override val context

    = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) { if(result.isSuccess) { println("suspend function returned: ${result.getOrNull()}") }else{ println("suspend function threw an exception ${result.exceptionOrNul } } } import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 6 7 8 9 10 11 12 13 14 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21
  3. val completionCallback = object : Continuation<Unit> { override val context

    = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) { if(result.isSuccess) { println("suspend function returned: ${result.getOrNull()}") }else{ println("suspend function threw an exception ${result.exceptionOrNul } } } import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 6 7 8 9 10 11 12 13 14 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 val completionCallback = object : Continuation<Unit> { import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21
  4. val completionCallback = object : Continuation<Unit> { override val context

    = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) { if(result.isSuccess) { println("suspend function returned: ${result.getOrNull()}") }else{ println("suspend function threw an exception ${result.exceptionOrNul } } } import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 6 7 8 9 10 11 12 13 14 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 val completionCallback = object : Continuation<Unit> { import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 override val context = EmptyCoroutineContext import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21
  5. val completionCallback = object : Continuation<Unit> { override val context

    = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) { if(result.isSuccess) { println("suspend function returned: ${result.getOrNull()}") }else{ println("suspend function threw an exception ${result.exceptionOrNul } } } import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 6 7 8 9 10 11 12 13 14 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 val completionCallback = object : Continuation<Unit> { import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 override val context = EmptyCoroutineContext import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 override fun resumeWith(result: Result<Unit>) { import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 override val context = EmptyCoroutineContext 7 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21
  6. val completionCallback = object : Continuation<Unit> { override val context

    = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) { if(result.isSuccess) { println("suspend function returned: ${result.getOrNull()}") }else{ println("suspend function threw an exception ${result.exceptionOrNul } } } import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 6 7 8 9 10 11 12 13 14 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 val completionCallback = object : Continuation<Unit> { import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 override val context = EmptyCoroutineContext import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 override fun resumeWith(result: Result<Unit>) { import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 override val context = EmptyCoroutineContext 7 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 if(result.isSuccess) { import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21
  7. val completionCallback = object : Continuation<Unit> { override val context

    = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) { if(result.isSuccess) { println("suspend function returned: ${result.getOrNull()}") }else{ println("suspend function threw an exception ${result.exceptionOrNul } } } import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 6 7 8 9 10 11 12 13 14 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 val completionCallback = object : Continuation<Unit> { import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 override val context = EmptyCoroutineContext import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 override fun resumeWith(result: Result<Unit>) { import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 override val context = EmptyCoroutineContext 7 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 if(result.isSuccess) { import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 println("suspend function returned: ${result.getOrNull()}") import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21
  8. val completionCallback = object : Continuation<Unit> { override val context

    = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) { if(result.isSuccess) { println("suspend function returned: ${result.getOrNull()}") }else{ println("suspend function threw an exception ${result.exceptionOrNul } } } import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 6 7 8 9 10 11 12 13 14 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 val completionCallback = object : Continuation<Unit> { import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 override val context = EmptyCoroutineContext import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 override fun resumeWith(result: Result<Unit>) { import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 override val context = EmptyCoroutineContext 7 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 if(result.isSuccess) { import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 println("suspend function returned: ${result.getOrNull()}") import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 println("suspend function threw an exception ${result.exceptionOrNul import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21
  9. val completionCallback = object : Continuation<Unit> { override val context

    = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) { if(result.isSuccess) { println("suspend function returned: ${result.getOrNull()}") }else{ println("suspend function threw an exception ${result.exceptionOrNul } } } import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 6 7 8 9 10 11 12 13 14 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 val completionCallback = object : Continuation<Unit> { import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 override val context = EmptyCoroutineContext import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 override fun resumeWith(result: Result<Unit>) { import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 override val context = EmptyCoroutineContext 7 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 if(result.isSuccess) { import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 println("suspend function returned: ${result.getOrNull()}") import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 println("suspend function threw an exception ${result.exceptionOrNul import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 12 } 13 } 14 } 15 ::greet.createCoroutineUnintercepted(completionCallback) 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 ::greet.createCoroutineUnintercepted(completionCallback) import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3 4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21
  10. ::greet.createCoroutineUnintercepted(completionCallback).resumeWith(Result.succe import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3

    4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21
  11. ::greet.createCoroutineUnintercepted(completionCallback).resumeWith(Result.succe import kotlin.coroutines.intrinsics.createCoroutineUnintercepted 1 import kotlin.coroutines.Continuation 2 import kotlin.coroutines.EmptyCoroutineContext 3

    4 fun main() { 5 val completionCallback = object : Continuation<Unit> { 6 override val context = EmptyCoroutineContext 7 override fun resumeWith(result: Result<Unit>) { 8 if(result.isSuccess) { 9 println("suspend function returned: ${result.getOrNull()}") 10 }else{ 11 println("suspend function threw an exception ${result.exceptionOrNul 12 } 13 } 14 } 15 16 } 17 18 suspend fun greet() { 19 println("Hello!") 20 } 21 import kotlin.coroutines.intrinsics.createCoroutineUnintercepted import kotlin.coroutines.Continuation import kotlin.coroutines.EmptyCoroutineContext fun main() { val completionCallback = object : Continuation<Unit> { override val context = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) { if(result.isSuccess) { println("suspend function returned: ${result.getOrNull()}") }else{ println("suspend function threw an exception ${result.exceptionOrNul } } } ::greet.createCoroutineUnintercepted(completionCallback).resumeWith(Result.succe } suspend fun greet() { println("Hello!") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
  12. fun main(){ greet() } fun greet() { println("Hello!") } 1

    2 3 4 5 6 7 fun greet() { println("Hello!") } fun main(){ 1 greet() 2 } 3 4 5 6 7
  13. fun main(){ doSomething() } suspend fun doSomething(){ doSomethingElse() doSomethingElse() }

    suspend fun doSomethingElse() { println("Hello!") } 1 2 3 4 5 6 7 8 9 10 11 12
  14. fun main(){ doSomething() } suspend fun doSomething(){ doSomethingElse() doSomethingElse() }

    suspend fun doSomethingElse() { Toast.makeText(..) } 1 2 3 4 5 6 7 8 9 10 11 12
  15. fun main(){ doSomething() } suspend fun doSomething(){ doSomethingElse() doSomethingElse() }

    suspend fun doSomethingElse() { File("...").readContents() } 1 2 3 4 5 6 7 8 9 10 11 12
  16. fun main(){ doSomething() } suspend fun doSomething(){ doSomethingElse() doSomethingElse() }

    suspend fun doSomethingElse() { db.deleteAllMessages() } 1 2 3 4 5 6 7 8 9 10 11 12
  17. fun main(){ doSomething() } suspend fun doSomething(){ doSomethingElse() doSomethingElse() }

    suspend fun doSomethingElse() { doSomething() } 1 2 3 4 5 6 7 8 9 10 11 12
  18. fun main(){ doSomething() } suspend fun doSomething(){ doSomethingElse() doSomethingElse() }

    fun doSomethingElse() { doSomething() } 1 2 3 4 5 6 7 8 9 10 11 12
  19. fun main(){ doSomething() } fun doSomething(){ doSomethingElse() doSomethingElse() } fun

    doSomethingElse() { doSomething() } 1 2 3 4 5 6 7 8 9 10 11 12
  20. @SinceKotlin("1.3") @InlineOnly @Suppress("UNUSED_PARAMETER", "RedundantSuspendModifier") public suspend inline fun <T> suspendCoroutineUninterceptedOrReturn(crossinline

    block: (Con contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } throw NotImplementedError("Implementation of suspendCoroutineUninterceptedOrReturn } 1 2 3 4 5 6 7
  21. suspend fun fun1(){ println("1.1") fun2() println("1.2") } suspend fun fun2(){

    println("2.1") fun3() println("2.2") } suspend fun fun3(){ println("3.1") suspendCoroutineUnInterceptedOrReturn<Unit> { cont -> thread{ Thread.sleep(2000) cont.resumeWith(Result.success(Unit)) } COROUTINE_SUSPENDED } println("3.2") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
  22. fun fun1(cont: () -> Unit){ println("1.1") fun2{ println("1.2") cont() }

    } fun fun2(cont: () -> Unit){ println("2.1") fun3{ println("2.2") cont() } } fun fun3(cont: () -> Unit){ println("3.1") thread{ Thread.sleep(2000) println("3.2") cont() } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
  23. fun fun1(cont: Continuation<Unit>){ println("1.1") fun2{ println("1.2") cont.resumeWith(...) } } fun

    fun2(cont: Continuation<Unit>){ println("2.1") fun3{ println("2.2") cont.resumeWith(...) } } fun fun3(cont: Continuation<Unit>){ println("3.1") thread{ Thread.sleep(2000) println("3.2") cont.resumeWith(...) } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
  24. suspend fun networkCall() { println("Calling API ") val responseCode =

    suspendCoroutineUninterceptedOrReturn<Int> { cont -> thread{ try { val connection = URL("https://google.com").openConnection() connection.requestMethod = "GET" cont.resumeWith(Result.success(connection.responseCode)) } catch (e: Exception) { cont.resumeWith(Result.error(e)) } } COROUTINE_SUSPENDED } println("Server responded with $responseCode") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  25. suspend fun doSomething() { ... networkCall() ... } suspend fun

    networkCall() { println("Calling API ") val responseCode = suspendCoroutineUninterceptedOrReturn<Int> { cont -> thread{ try { val connection = URL("https://google.com").openConnection() connection.requestMethod = "GET" cont.resumeWith(Result.success(connection.responseCode)) } catch (e: Exception) { cont.resumeWith(Result.error(e)) } } COROUTINE_SUSPENDED } println("Server responded with $responseCode") 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
  26. fun launch(block: suspend () -> Unit) { 1 val completionCallback

    = object : Continuation<Unit> { 2 override val context: CoroutineContext = EmptyCoroutineContext 3 override fun resumeWith(result: Result<Unit>) {} 4 } 5 block.createCoroutineUnintercepted(completionCallback).resumeWith(Result.suc 6 } 7
  27. fun launch(block: suspend () -> Unit) { 1 val completionCallback

    = object : Continuation<Unit> { 2 override val context: CoroutineContext = EmptyCoroutineContext 3 override fun resumeWith(result: Result<Unit>) {} 4 } 5 block.createCoroutineUnintercepted(completionCallback).resumeWith(Result.suc 6 } 7 val completionCallback = object : Continuation<Unit> { fun launch(block: suspend () -> Unit) { 1 2 override val context: CoroutineContext = EmptyCoroutineContext 3 override fun resumeWith(result: Result<Unit>) {} 4 } 5 block.createCoroutineUnintercepted(completionCallback).resumeWith(Result.suc 6 } 7
  28. fun launch(block: suspend () -> Unit) { 1 val completionCallback

    = object : Continuation<Unit> { 2 override val context: CoroutineContext = EmptyCoroutineContext 3 override fun resumeWith(result: Result<Unit>) {} 4 } 5 block.createCoroutineUnintercepted(completionCallback).resumeWith(Result.suc 6 } 7 val completionCallback = object : Continuation<Unit> { fun launch(block: suspend () -> Unit) { 1 2 override val context: CoroutineContext = EmptyCoroutineContext 3 override fun resumeWith(result: Result<Unit>) {} 4 } 5 block.createCoroutineUnintercepted(completionCallback).resumeWith(Result.suc 6 } 7 override val context: CoroutineContext = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) {} fun launch(block: suspend () -> Unit) { 1 val completionCallback = object : Continuation<Unit> { 2 3 4 } 5 block.createCoroutineUnintercepted(completionCallback).resumeWith(Result.suc 6 } 7
  29. fun launch(block: suspend () -> Unit) { 1 val completionCallback

    = object : Continuation<Unit> { 2 override val context: CoroutineContext = EmptyCoroutineContext 3 override fun resumeWith(result: Result<Unit>) {} 4 } 5 block.createCoroutineUnintercepted(completionCallback).resumeWith(Result.suc 6 } 7 val completionCallback = object : Continuation<Unit> { fun launch(block: suspend () -> Unit) { 1 2 override val context: CoroutineContext = EmptyCoroutineContext 3 override fun resumeWith(result: Result<Unit>) {} 4 } 5 block.createCoroutineUnintercepted(completionCallback).resumeWith(Result.suc 6 } 7 override val context: CoroutineContext = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) {} fun launch(block: suspend () -> Unit) { 1 val completionCallback = object : Continuation<Unit> { 2 3 4 } 5 block.createCoroutineUnintercepted(completionCallback).resumeWith(Result.suc 6 } 7 block.createCoroutineUnintercepted(completionCallback).resumeWith(Result.suc fun launch(block: suspend () -> Unit) { 1 val completionCallback = object : Continuation<Unit> { 2 override val context: CoroutineContext = EmptyCoroutineContext 3 override fun resumeWith(result: Result<Unit>) {} 4 } 5 6 } 7
  30. fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1

    2 val completionCallback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 override fun resumeWith(result: Result<Unit>) {} 5 } 6 7 val continuation = block.createCoroutineUnintercepted(completionCallback) 8 9 when (dispatcher) { 10 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 11 continuation.resumeWith(Result.success(Unit)) 12 } 13 14 Dispatchers.Background -> thread { 15 continuation.resumeWith(Result.success(Unit)) 16 } 17 } 18 } 19
  31. fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1

    2 val completionCallback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 override fun resumeWith(result: Result<Unit>) {} 5 } 6 7 val continuation = block.createCoroutineUnintercepted(completionCallback) 8 9 when (dispatcher) { 10 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 11 continuation.resumeWith(Result.success(Unit)) 12 } 13 14 Dispatchers.Background -> thread { 15 continuation.resumeWith(Result.success(Unit)) 16 } 17 } 18 } 19 val completionCallback = object : Continuation<Unit> { override val context = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) {} } val continuation = block.createCoroutineUnintercepted(completionCallback) fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1 2 3 4 5 6 7 8 9 when (dispatcher) { 10 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 11 continuation.resumeWith(Result.success(Unit)) 12 } 13 14 Dispatchers.Background -> thread { 15 continuation.resumeWith(Result.success(Unit)) 16 } 17 } 18 } 19
  32. fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1

    2 val completionCallback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 override fun resumeWith(result: Result<Unit>) {} 5 } 6 7 val continuation = block.createCoroutineUnintercepted(completionCallback) 8 9 when (dispatcher) { 10 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 11 continuation.resumeWith(Result.success(Unit)) 12 } 13 14 Dispatchers.Background -> thread { 15 continuation.resumeWith(Result.success(Unit)) 16 } 17 } 18 } 19 val completionCallback = object : Continuation<Unit> { override val context = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) {} } val continuation = block.createCoroutineUnintercepted(completionCallback) fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1 2 3 4 5 6 7 8 9 when (dispatcher) { 10 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 11 continuation.resumeWith(Result.success(Unit)) 12 } 13 14 Dispatchers.Background -> thread { 15 continuation.resumeWith(Result.success(Unit)) 16 } 17 } 18 } 19 when (dispatcher) { Dispatchers.Main -> Handler(Looper.getMainLooper()).post { continuation.resumeWith(Result.success(Unit)) } Dispatchers.Background -> thread { continuation.resumeWith(Result.success(Unit)) } fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1 2 val completionCallback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 override fun resumeWith(result: Result<Unit>) {} 5 } 6 7 val continuation = block.createCoroutineUnintercepted(completionCallback) 8 9 10 11 12 13 14 15 16 17 } 18 } 19
  33. fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1

    2 val completionCallback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 override fun resumeWith(result: Result<Unit>) {} 5 } 6 7 val continuation = block.createCoroutineUnintercepted(completionCallback) 8 9 when (dispatcher) { 10 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 11 continuation.resumeWith(Result.success(Unit)) 12 } 13 14 Dispatchers.Background -> thread { 15 continuation.resumeWith(Result.success(Unit)) 16 } 17 } 18 } 19 val completionCallback = object : Continuation<Unit> { override val context = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) {} } val continuation = block.createCoroutineUnintercepted(completionCallback) fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1 2 3 4 5 6 7 8 9 when (dispatcher) { 10 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 11 continuation.resumeWith(Result.success(Unit)) 12 } 13 14 Dispatchers.Background -> thread { 15 continuation.resumeWith(Result.success(Unit)) 16 } 17 } 18 } 19 when (dispatcher) { Dispatchers.Main -> Handler(Looper.getMainLooper()).post { continuation.resumeWith(Result.success(Unit)) } Dispatchers.Background -> thread { continuation.resumeWith(Result.success(Unit)) } fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1 2 val completionCallback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 override fun resumeWith(result: Result<Unit>) {} 5 } 6 7 val continuation = block.createCoroutineUnintercepted(completionCallback) 8 9 10 11 12 13 14 15 16 17 } 18 } 19 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { continuation.resumeWith(Result.success(Unit)) } fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1 2 val completionCallback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 override fun resumeWith(result: Result<Unit>) {} 5 } 6 7 val continuation = block.createCoroutineUnintercepted(completionCallback) 8 9 when (dispatcher) { 10 11 12 13 14 Dispatchers.Background -> thread { 15 continuation.resumeWith(Result.success(Unit)) 16 } 17 } 18 } 19
  34. fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1

    2 val completionCallback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 override fun resumeWith(result: Result<Unit>) {} 5 } 6 7 val continuation = block.createCoroutineUnintercepted(completionCallback) 8 9 when (dispatcher) { 10 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 11 continuation.resumeWith(Result.success(Unit)) 12 } 13 14 Dispatchers.Background -> thread { 15 continuation.resumeWith(Result.success(Unit)) 16 } 17 } 18 } 19 val completionCallback = object : Continuation<Unit> { override val context = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) {} } val continuation = block.createCoroutineUnintercepted(completionCallback) fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1 2 3 4 5 6 7 8 9 when (dispatcher) { 10 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 11 continuation.resumeWith(Result.success(Unit)) 12 } 13 14 Dispatchers.Background -> thread { 15 continuation.resumeWith(Result.success(Unit)) 16 } 17 } 18 } 19 when (dispatcher) { Dispatchers.Main -> Handler(Looper.getMainLooper()).post { continuation.resumeWith(Result.success(Unit)) } Dispatchers.Background -> thread { continuation.resumeWith(Result.success(Unit)) } fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1 2 val completionCallback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 override fun resumeWith(result: Result<Unit>) {} 5 } 6 7 val continuation = block.createCoroutineUnintercepted(completionCallback) 8 9 10 11 12 13 14 15 16 17 } 18 } 19 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { continuation.resumeWith(Result.success(Unit)) } fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1 2 val completionCallback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 override fun resumeWith(result: Result<Unit>) {} 5 } 6 7 val continuation = block.createCoroutineUnintercepted(completionCallback) 8 9 when (dispatcher) { 10 11 12 13 14 Dispatchers.Background -> thread { 15 continuation.resumeWith(Result.success(Unit)) 16 } 17 } 18 } 19 Dispatchers.Background -> thread { continuation.resumeWith(Result.success(Unit)) } fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1 2 val completionCallback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 override fun resumeWith(result: Result<Unit>) {} 5 } 6 7 val continuation = block.createCoroutineUnintercepted(completionCallback) 8 9 when (dispatcher) { 10 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 11 continuation.resumeWith(Result.success(Unit)) 12 } 13 14 15 16 17 } 18 } 19
  35. button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 textView.text = "Downloading file" 3

    suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 cont.resumeWith(Result.success(Unit)) 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13
  36. button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 textView.text = "Downloading file" 3

    suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 cont.resumeWith(Result.success(Unit)) 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 launch(Dispatchers.Main) { button.setOnClickListener{ 1 2 textView.text = "Downloading file" 3 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 cont.resumeWith(Result.success(Unit)) 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13
  37. button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 textView.text = "Downloading file" 3

    suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 cont.resumeWith(Result.success(Unit)) 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 launch(Dispatchers.Main) { button.setOnClickListener{ 1 2 textView.text = "Downloading file" 3 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 cont.resumeWith(Result.success(Unit)) 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 textView.text = "Downloading file" button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 3 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 cont.resumeWith(Result.success(Unit)) 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13
  38. button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 textView.text = "Downloading file" 3

    suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 cont.resumeWith(Result.success(Unit)) 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 launch(Dispatchers.Main) { button.setOnClickListener{ 1 2 textView.text = "Downloading file" 3 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 cont.resumeWith(Result.success(Unit)) 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 textView.text = "Downloading file" button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 3 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 cont.resumeWith(Result.success(Unit)) 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> thread { downloader.download("http://example.com/file.txt") cont.resumeWith(Result.success(Unit)) } COROUTINE_SUSPENDED } button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 textView.text = "Downloading file" 3 4 5 6 7 8 9 10 textView.text = "Download Complete" 11 } 12 } 13
  39. button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 textView.text = "Downloading file" 3

    suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 cont.resumeWith(Result.success(Unit)) 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 launch(Dispatchers.Main) { button.setOnClickListener{ 1 2 textView.text = "Downloading file" 3 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 cont.resumeWith(Result.success(Unit)) 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 textView.text = "Downloading file" button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 3 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 cont.resumeWith(Result.success(Unit)) 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> thread { downloader.download("http://example.com/file.txt") cont.resumeWith(Result.success(Unit)) } COROUTINE_SUSPENDED } button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 textView.text = "Downloading file" 3 4 5 6 7 8 9 10 textView.text = "Download Complete" 11 } 12 } 13 textView.text = "Download Complete" button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 textView.text = "Downloading file" 3 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 cont.resumeWith(Result.success(Unit)) 7 } 8 COROUTINE_SUSPENDED 9 } 10 11 } 12 } 13
  40. fun fun1(cont: Continuation<Unit>){ println("1.1") fun2{ println("1.2") cont() } } fun

    fun2(cont: Continuation<Unit>){ println("2.1") fun3{ println("2.2") cont() } } fun fun3(cont: Continuation<Unit>){ println("3.1") thread{ Thread.sleep(2000) println("3.2") cont() } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
  41. fun fun1(cont: Continuation<Unit>){ println("1.1") fun2{ println("1.2") cont() } } fun

    fun2(cont: Continuation<Unit>){ println("2.1") fun3{ println("2.2") cont() } } fun fun3(cont: Continuation<Unit>){ println("3.1") thread{ Thread.sleep(2000) println("3.2") cont() } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 fun fun3(cont: Continuation<Unit>){ println("3.1") thread{ Thread.sleep(2000) println("3.2") cont() } } fun fun1(cont: Continuation<Unit>){ 1 println("1.1") 2 fun2{ 3 println("1.2") 4 cont() 5 } 6 } 7 8 fun fun2(cont: Continuation<Unit>){ 9 println("2.1") 10 fun3{ 11 println("2.2") 12 cont() 13 } 14 } 15 16 17 18 19 20 21 22 23 24
  42. suspend fun doSomething(){ suspendCoroutineUninterceptedOrReturn<Unit> { cont -> thread { downloader.download("http://example.com/file.txt

    cont.resumeWith(Result.success(Unit)) } COROUTINE_SUSPENDED } } 1 2 3 4 5 6 7 8 9 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> suspend fun doSomething(){ 1 2 thread { 3 downloader.download("http://example.com/file.txt 4 cont.resumeWith(Result.success(Unit)) 5 } 6 COROUTINE_SUSPENDED 7 } 8 } 9
  43. object DispatcherKey: CoroutineContext.Key<Dispatchers>{} sealed class Dispatchers: CoroutineContext.Element { object Main

    : Dispatchers() object Background : Dispatchers() override val key = DispatcherKey } 1 2 3 4 5 6
  44. object DispatcherKey: CoroutineContext.Key<Dispatchers>{} sealed class Dispatchers: CoroutineContext.Element { object Main

    : Dispatchers() object Background : Dispatchers() override val key = DispatcherKey } 1 2 3 4 5 6 object DispatcherKey: CoroutineContext.Key<Dispatchers>{} 1 sealed class Dispatchers: CoroutineContext.Element { 2 object Main : Dispatchers() 3 object Background : Dispatchers() 4 override val key = DispatcherKey 5 } 6
  45. object DispatcherKey: CoroutineContext.Key<Dispatchers>{} sealed class Dispatchers: CoroutineContext.Element { object Main

    : Dispatchers() object Background : Dispatchers() override val key = DispatcherKey } 1 2 3 4 5 6 object DispatcherKey: CoroutineContext.Key<Dispatchers>{} 1 sealed class Dispatchers: CoroutineContext.Element { 2 object Main : Dispatchers() 3 object Background : Dispatchers() 4 override val key = DispatcherKey 5 } 6 sealed class Dispatchers: CoroutineContext.Element { object DispatcherKey: CoroutineContext.Key<Dispatchers>{} 1 2 object Main : Dispatchers() 3 object Background : Dispatchers() 4 override val key = DispatcherKey 5 } 6
  46. object DispatcherKey: CoroutineContext.Key<Dispatchers>{} sealed class Dispatchers: CoroutineContext.Element { object Main

    : Dispatchers() object Background : Dispatchers() override val key = DispatcherKey } 1 2 3 4 5 6 object DispatcherKey: CoroutineContext.Key<Dispatchers>{} 1 sealed class Dispatchers: CoroutineContext.Element { 2 object Main : Dispatchers() 3 object Background : Dispatchers() 4 override val key = DispatcherKey 5 } 6 sealed class Dispatchers: CoroutineContext.Element { object DispatcherKey: CoroutineContext.Key<Dispatchers>{} 1 2 object Main : Dispatchers() 3 object Background : Dispatchers() 4 override val key = DispatcherKey 5 } 6 override val key = DispatcherKey object DispatcherKey: CoroutineContext.Key<Dispatchers>{} 1 sealed class Dispatchers: CoroutineContext.Element { 2 object Main : Dispatchers() 3 object Background : Dispatchers() 4 5 } 6
  47. object DispatcherKey: CoroutineContext.Key<Dispatchers>{} sealed class Dispatchers: CoroutineContext.Element { object Main

    : Dispatchers() object Background : Dispatchers() override val key = DispatcherKey } val combined = EmptyCoroutineContext + Dispatchers.Main val original = combined.get(DispatcherKey) 1 2 3 4 5 6 7 8 9
  48. sealed class Dispatchers: CoroutineContext.Element { object Main : Dispatchers() object

    Background : Dispatchers() override val key = DispatcherKey companion object DispatcherKey: CoroutineContext.Key<Dis } val combined = EmptyCoroutineContext + Dispatchers.Main val original = combined.get(Dispatchers) 1 2 3 4 5 6 7 8 9
  49. sealed class Dispatchers: CoroutineContext.Element { object Main : Dispatchers() object

    Background : Dispatchers() override val key = DispatcherKey companion object DispatcherKey: CoroutineContext.Key<Dis } val combined = EmptyCoroutineContext + Dispatchers.Main val original = combined.get(Dispatchers) 1 2 3 4 5 6 7 8 9 val original = combined.get(Dispatchers) sealed class Dispatchers: CoroutineContext.Element { 1 object Main : Dispatchers() 2 object Background : Dispatchers() 3 override val key = DispatcherKey 4 companion object DispatcherKey: CoroutineContext.Key<Dis 5 } 6 7 val combined = EmptyCoroutineContext + Dispatchers.Main 8 9
  50. val original = combined[Dispatchers] sealed class Dispatchers: CoroutineContext.Element { 1

    object Main : Dispatchers() 2 object Background : Dispatchers() 3 override val key = DispatcherKey 4 companion object DispatcherKey: CoroutineContext.Key<Dis 5 } 6 7 val combined = EmptyCoroutineContext + Dispatchers.Main 8 9
  51. suspendCoroutineUninterceptedOrReturn<Unit> { cont -> thread { when (cont.context[Dispatchers]) { Dispatchers.Main

    -> Handler(Looper.getMainLooper( cont.resumeWith(Result.success(Unit)) } Dispatchers.Background -> thread { cont.resumeWith(Result.success(Unit)) } } } COROUTINE_SUSPENDED } 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  52. suspendCoroutineUninterceptedOrReturn<Unit> { cont -> thread { when (cont.context[Dispatchers]) { Dispatchers.Main

    -> Handler(Looper.getMainLooper( cont.resumeWith(Result.success(Unit)) } Dispatchers.Background -> thread { cont.resumeWith(Result.success(Unit)) } } } COROUTINE_SUSPENDED } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 when (cont.context[Dispatchers]) { suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 1 thread { 2 3 Dispatchers.Main -> Handler(Looper.getMainLooper( 4 cont.resumeWith(Result.success(Unit)) 5 } 6 7 Dispatchers.Background -> thread { 8 cont.resumeWith(Result.success(Unit)) 9 } 10 } 11 } 12 COROUTINE_SUSPENDED 13 } 14
  53. suspendCoroutineUninterceptedOrReturn<Unit> { cont -> thread { when (cont.context[Dispatchers]) { Dispatchers.Main

    -> Handler(Looper.getMainLooper( cont.resumeWith(Result.success(Unit)) } Dispatchers.Background -> thread { cont.resumeWith(Result.success(Unit)) } } } COROUTINE_SUSPENDED } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 when (cont.context[Dispatchers]) { suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 1 thread { 2 3 Dispatchers.Main -> Handler(Looper.getMainLooper( 4 cont.resumeWith(Result.success(Unit)) 5 } 6 7 Dispatchers.Background -> thread { 8 cont.resumeWith(Result.success(Unit)) 9 } 10 } 11 } 12 COROUTINE_SUSPENDED 13 } 14 Dispatchers.Main -> Handler(Looper.getMainLooper( cont.resumeWith(Result.success(Unit)) } suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 1 thread { 2 when (cont.context[Dispatchers]) { 3 4 5 6 7 Dispatchers.Background -> thread { 8 cont.resumeWith(Result.success(Unit)) 9 } 10 } 11 } 12 COROUTINE_SUSPENDED 13 } 14
  54. suspendCoroutineUninterceptedOrReturn<Unit> { cont -> thread { when (cont.context[Dispatchers]) { Dispatchers.Main

    -> Handler(Looper.getMainLooper( cont.resumeWith(Result.success(Unit)) } Dispatchers.Background -> thread { cont.resumeWith(Result.success(Unit)) } } } COROUTINE_SUSPENDED } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 when (cont.context[Dispatchers]) { suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 1 thread { 2 3 Dispatchers.Main -> Handler(Looper.getMainLooper( 4 cont.resumeWith(Result.success(Unit)) 5 } 6 7 Dispatchers.Background -> thread { 8 cont.resumeWith(Result.success(Unit)) 9 } 10 } 11 } 12 COROUTINE_SUSPENDED 13 } 14 Dispatchers.Main -> Handler(Looper.getMainLooper( cont.resumeWith(Result.success(Unit)) } suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 1 thread { 2 when (cont.context[Dispatchers]) { 3 4 5 6 7 Dispatchers.Background -> thread { 8 cont.resumeWith(Result.success(Unit)) 9 } 10 } 11 } 12 COROUTINE_SUSPENDED 13 } 14 Dispatchers.Background -> thread { cont.resumeWith(Result.success(Unit)) } suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 1 thread { 2 when (cont.context[Dispatchers]) { 3 Dispatchers.Main -> Handler(Looper.getMainLooper( 4 cont.resumeWith(Result.success(Unit)) 5 } 6 7 8 9 10 } 11 } 12 COROUTINE_SUSPENDED 13 } 14
  55. class DispatchedContinuation<T>( override val context: CoroutineContext, val continuation: Continuation<T> )

    : Continuation<T> { override fun resumeWith(result: Result<T>) { when (context as Dispatcher) { Dispatchers.Main -> Handler(Looper.getMainLoope continuation.resumeWith(result) } Dispatchers.Background -> thread { continuation.resumeWith(result) } } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  56. fun launch(dispatcher: Dispatcher, block: suspend () -> Unit) { val

    callback = object : Continuation<Unit> { override val context = dispatcher override fun resumeWith(result: Result<Unit>) {} } val coroutine = block.createCoroutineUnintercepted(callback) DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.success } 1 2 3 4 5 6 7 8 9
  57. button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 textView.text = "Downloading file" 3

    suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 DispatchedContinuation(cont.context,cont).resumeWith(Resul 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13
  58. button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 textView.text = "Downloading file" 3

    suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 DispatchedContinuation(cont.context,cont).resumeWith(Resul 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 launch(Dispatchers.Main) { button.setOnClickListener{ 1 2 textView.text = "Downloading file" 3 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 DispatchedContinuation(cont.context,cont).resumeWith(Resul 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13
  59. button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 textView.text = "Downloading file" 3

    suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 DispatchedContinuation(cont.context,cont).resumeWith(Resul 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 launch(Dispatchers.Main) { button.setOnClickListener{ 1 2 textView.text = "Downloading file" 3 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 DispatchedContinuation(cont.context,cont).resumeWith(Resul 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 textView.text = "Downloading file" button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 3 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 DispatchedContinuation(cont.context,cont).resumeWith(Resul 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13
  60. button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 textView.text = "Downloading file" 3

    suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 DispatchedContinuation(cont.context,cont).resumeWith(Resul 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 launch(Dispatchers.Main) { button.setOnClickListener{ 1 2 textView.text = "Downloading file" 3 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 DispatchedContinuation(cont.context,cont).resumeWith(Resul 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 textView.text = "Downloading file" button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 3 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 DispatchedContinuation(cont.context,cont).resumeWith(Resul 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> thread { downloader.download("http://example.com/file.txt") DispatchedContinuation(cont.context,cont).resumeWith(Resul } COROUTINE_SUSPENDED } button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 textView.text = "Downloading file" 3 4 5 6 7 8 9 10 textView.text = "Download Complete" 11 } 12 } 13
  61. button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 textView.text = "Downloading file" 3

    suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 DispatchedContinuation(cont.context,cont).resumeWith(Resul 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 launch(Dispatchers.Main) { button.setOnClickListener{ 1 2 textView.text = "Downloading file" 3 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 DispatchedContinuation(cont.context,cont).resumeWith(Resul 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 textView.text = "Downloading file" button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 3 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 DispatchedContinuation(cont.context,cont).resumeWith(Resul 7 } 8 COROUTINE_SUSPENDED 9 } 10 textView.text = "Download Complete" 11 } 12 } 13 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> thread { downloader.download("http://example.com/file.txt") DispatchedContinuation(cont.context,cont).resumeWith(Resul } COROUTINE_SUSPENDED } button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 textView.text = "Downloading file" 3 4 5 6 7 8 9 10 textView.text = "Download Complete" 11 } 12 } 13 textView.text = "Download Complete" button.setOnClickListener{ 1 launch(Dispatchers.Main) { 2 textView.text = "Downloading file" 3 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 4 thread { 5 downloader.download("http://example.com/file.txt") 6 DispatchedContinuation(cont.context,cont).resumeWith(Resul 7 } 8 COROUTINE_SUSPENDED 9 } 10 11 } 12 } 13
  62. class DispatchedContinuation<T>( override val context: CoroutineContext, val continuation: Continuation<T> )

    : Continuation<T> { override fun resumeWith(result: Result<T>) { when (context as Dispatcher) { Dispatchers.Main -> Handler(Looper.getMainLooper()).post { continuation.resumeWith(result) } Dispatchers.Background -> thread { continuation.resumeWith(result) } } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  63. Dispatchers.Main -> Handler(Looper.getMainLooper()).post { continuation.resumeWith(result) } class DispatchedContinuation<T>( 1 override

    val context: CoroutineContext, 2 val continuation: Continuation<T> 3 ) : Continuation<T> { 4 override fun resumeWith(result: Result<T>) { 5 when (context as Dispatcher) { 6 7 8 9 10 Dispatchers.Default -> defaultThreadPool.submit { 11 continuation.resumeWith(result) 12 } 13 14 Dispatchers.IO -> ioThreadPool.submit { 15 continuation.resumeWith(result) 16 } 17 18 Dispatchers.Unconfined -> continuation.resumeWith(result) 19 20 Dispatchers.MainImmediate -> { 21 if(Looper.getMainLooper() == Looper.myLooper()){ 22 continuation.resumeWith(result) 23 }else{ 24 Handler(Looper.getMainLooper()).post { 25 continuation.resumeWith(result) 26 } 27 } 28 } 29
  64. Dispatchers.Main -> Handler(Looper.getMainLooper()).post { continuation.resumeWith(result) } class DispatchedContinuation<T>( 1 override

    val context: CoroutineContext, 2 val continuation: Continuation<T> 3 ) : Continuation<T> { 4 override fun resumeWith(result: Result<T>) { 5 when (context as Dispatcher) { 6 7 8 9 10 Dispatchers.Default -> defaultThreadPool.submit { 11 continuation.resumeWith(result) 12 } 13 14 Dispatchers.IO -> ioThreadPool.submit { 15 continuation.resumeWith(result) 16 } 17 18 Dispatchers.Unconfined -> continuation.resumeWith(result) 19 20 Dispatchers.MainImmediate -> { 21 if(Looper.getMainLooper() == Looper.myLooper()){ 22 continuation.resumeWith(result) 23 }else{ 24 Handler(Looper.getMainLooper()).post { 25 continuation.resumeWith(result) 26 } 27 } 28 } 29 Dispatchers.Default -> defaultThreadPool.submit { continuation.resumeWith(result) } class DispatchedContinuation<T>( 1 override val context: CoroutineContext, 2 val continuation: Continuation<T> 3 ) : Continuation<T> { 4 override fun resumeWith(result: Result<T>) { 5 when (context as Dispatcher) { 6 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 7 continuation.resumeWith(result) 8 } 9 10 11 12 13 14 Dispatchers.IO -> ioThreadPool.submit { 15 continuation.resumeWith(result) 16 } 17 18 Dispatchers.Unconfined -> continuation.resumeWith(result) 19 20 Dispatchers.MainImmediate -> { 21 if(Looper.getMainLooper() == Looper.myLooper()){ 22 continuation.resumeWith(result) 23 }else{ 24 Handler(Looper.getMainLooper()).post { 25 continuation.resumeWith(result) 26 } 27 } 28 } 29
  65. Dispatchers.Main -> Handler(Looper.getMainLooper()).post { continuation.resumeWith(result) } class DispatchedContinuation<T>( 1 override

    val context: CoroutineContext, 2 val continuation: Continuation<T> 3 ) : Continuation<T> { 4 override fun resumeWith(result: Result<T>) { 5 when (context as Dispatcher) { 6 7 8 9 10 Dispatchers.Default -> defaultThreadPool.submit { 11 continuation.resumeWith(result) 12 } 13 14 Dispatchers.IO -> ioThreadPool.submit { 15 continuation.resumeWith(result) 16 } 17 18 Dispatchers.Unconfined -> continuation.resumeWith(result) 19 20 Dispatchers.MainImmediate -> { 21 if(Looper.getMainLooper() == Looper.myLooper()){ 22 continuation.resumeWith(result) 23 }else{ 24 Handler(Looper.getMainLooper()).post { 25 continuation.resumeWith(result) 26 } 27 } 28 } 29 Dispatchers.Default -> defaultThreadPool.submit { continuation.resumeWith(result) } class DispatchedContinuation<T>( 1 override val context: CoroutineContext, 2 val continuation: Continuation<T> 3 ) : Continuation<T> { 4 override fun resumeWith(result: Result<T>) { 5 when (context as Dispatcher) { 6 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 7 continuation.resumeWith(result) 8 } 9 10 11 12 13 14 Dispatchers.IO -> ioThreadPool.submit { 15 continuation.resumeWith(result) 16 } 17 18 Dispatchers.Unconfined -> continuation.resumeWith(result) 19 20 Dispatchers.MainImmediate -> { 21 if(Looper.getMainLooper() == Looper.myLooper()){ 22 continuation.resumeWith(result) 23 }else{ 24 Handler(Looper.getMainLooper()).post { 25 continuation.resumeWith(result) 26 } 27 } 28 } 29 Dispatchers.IO -> ioThreadPool.submit { continuation.resumeWith(result) } class DispatchedContinuation T ( 1 override val context: CoroutineContext, 2 val continuation: Continuation<T> 3 ) : Continuation<T> { 4 override fun resumeWith(result: Result<T>) { 5 when (context as Dispatcher) { 6 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 7 continuation.resumeWith(result) 8 } 9 10 Dispatchers.Default -> defaultThreadPool.submit { 11 continuation.resumeWith(result) 12 } 13 14 15 16 17 18 Dispatchers.Unconfined -> continuation.resumeWith(result) 19 20 Dispatchers.MainImmediate -> { 21 if(Looper.getMainLooper() == Looper.myLooper()){ 22 continuation.resumeWith(result) 23 }else{ 24 Handler(Looper.getMainLooper()).post { 25 continuation.resumeWith(result) 26 } 27 } 28 } 29 } 30
  66. Dispatchers.Main -> Handler(Looper.getMainLooper()).post { continuation.resumeWith(result) } class DispatchedContinuation<T>( 1 override

    val context: CoroutineContext, 2 val continuation: Continuation<T> 3 ) : Continuation<T> { 4 override fun resumeWith(result: Result<T>) { 5 when (context as Dispatcher) { 6 7 8 9 10 Dispatchers.Default -> defaultThreadPool.submit { 11 continuation.resumeWith(result) 12 } 13 14 Dispatchers.IO -> ioThreadPool.submit { 15 continuation.resumeWith(result) 16 } 17 18 Dispatchers.Unconfined -> continuation.resumeWith(result) 19 20 Dispatchers.MainImmediate -> { 21 if(Looper.getMainLooper() == Looper.myLooper()){ 22 continuation.resumeWith(result) 23 }else{ 24 Handler(Looper.getMainLooper()).post { 25 continuation.resumeWith(result) 26 } 27 } 28 } 29 Dispatchers.Default -> defaultThreadPool.submit { continuation.resumeWith(result) } class DispatchedContinuation<T>( 1 override val context: CoroutineContext, 2 val continuation: Continuation<T> 3 ) : Continuation<T> { 4 override fun resumeWith(result: Result<T>) { 5 when (context as Dispatcher) { 6 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 7 continuation.resumeWith(result) 8 } 9 10 11 12 13 14 Dispatchers.IO -> ioThreadPool.submit { 15 continuation.resumeWith(result) 16 } 17 18 Dispatchers.Unconfined -> continuation.resumeWith(result) 19 20 Dispatchers.MainImmediate -> { 21 if(Looper.getMainLooper() == Looper.myLooper()){ 22 continuation.resumeWith(result) 23 }else{ 24 Handler(Looper.getMainLooper()).post { 25 continuation.resumeWith(result) 26 } 27 } 28 } 29 Dispatchers.IO -> ioThreadPool.submit { continuation.resumeWith(result) } class DispatchedContinuation<T>( 1 override val context: CoroutineContext, 2 val continuation: Continuation<T> 3 ) : Continuation<T> { 4 override fun resumeWith(result: Result<T>) { 5 when (context as Dispatcher) { 6 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 7 continuation.resumeWith(result) 8 } 9 10 Dispatchers.Default -> defaultThreadPool.submit { 11 continuation.resumeWith(result) 12 } 13 14 15 16 17 18 Dispatchers.Unconfined -> continuation.resumeWith(result) 19 20 Dispatchers.MainImmediate -> { 21 if(Looper.getMainLooper() == Looper.myLooper()){ 22 continuation.resumeWith(result) 23 }else{ 24 Handler(Looper.getMainLooper()).post { 25 continuation.resumeWith(result) 26 } 27 } 28 } 29 Dispatchers.Unconfined -> continuation.resumeWith(result) ) : Continuation<T> { 4 override fun resumeWith(result: Result<T>) { 5 when (context as Dispatcher) { 6 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 7 continuation.resumeWith(result) 8 } 9 10 Dispatchers.Default -> defaultThreadPool.submit { 11 continuation.resumeWith(result) 12 } 13 14 Dispatchers.IO -> ioThreadPool.submit { 15 continuation.resumeWith(result) 16 } 17 18 19 20 Dispatchers.MainImmediate -> { 21 if(Looper.getMainLooper() == Looper.myLooper()){ 22 continuation.resumeWith(result) 23 }else{ 24 Handler(Looper.getMainLooper()).post { 25 continuation.resumeWith(result) 26 } 27 } 28 } 29 } 30 } 31 } 32
  67. Dispatchers.Main -> Handler(Looper.getMainLooper()).post { continuation.resumeWith(result) } class DispatchedContinuation<T>( 1 override

    val context: CoroutineContext, 2 val continuation: Continuation<T> 3 ) : Continuation<T> { 4 override fun resumeWith(result: Result<T>) { 5 when (context as Dispatcher) { 6 7 8 9 10 Dispatchers.Default -> defaultThreadPool.submit { 11 continuation.resumeWith(result) 12 } 13 14 Dispatchers.IO -> ioThreadPool.submit { 15 continuation.resumeWith(result) 16 } 17 18 Dispatchers.Unconfined -> continuation.resumeWith(result) 19 20 Dispatchers.MainImmediate -> { 21 if(Looper.getMainLooper() == Looper.myLooper()){ 22 continuation.resumeWith(result) 23 }else{ 24 Handler(Looper.getMainLooper()).post { 25 continuation.resumeWith(result) 26 } 27 } 28 } 29 Dispatchers.Default -> defaultThreadPool.submit { continuation.resumeWith(result) } class DispatchedContinuation<T>( 1 override val context: CoroutineContext, 2 val continuation: Continuation<T> 3 ) : Continuation<T> { 4 override fun resumeWith(result: Result<T>) { 5 when (context as Dispatcher) { 6 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 7 continuation.resumeWith(result) 8 } 9 10 11 12 13 14 Dispatchers.IO -> ioThreadPool.submit { 15 continuation.resumeWith(result) 16 } 17 18 Dispatchers.Unconfined -> continuation.resumeWith(result) 19 20 Dispatchers.MainImmediate -> { 21 if(Looper.getMainLooper() == Looper.myLooper()){ 22 continuation.resumeWith(result) 23 }else{ 24 Handler(Looper.getMainLooper()).post { 25 continuation.resumeWith(result) 26 } 27 } 28 } 29 Dispatchers.IO -> ioThreadPool.submit { continuation.resumeWith(result) } class DispatchedContinuation<T>( 1 override val context: CoroutineContext, 2 val continuation: Continuation<T> 3 ) : Continuation<T> { 4 override fun resumeWith(result: Result<T>) { 5 when (context as Dispatcher) { 6 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 7 continuation.resumeWith(result) 8 } 9 10 Dispatchers.Default -> defaultThreadPool.submit { 11 continuation.resumeWith(result) 12 } 13 14 15 16 17 18 Dispatchers.Unconfined -> continuation.resumeWith(result) 19 20 Dispatchers.MainImmediate -> { 21 if(Looper.getMainLooper() == Looper.myLooper()){ 22 continuation.resumeWith(result) 23 }else{ 24 Handler(Looper.getMainLooper()).post { 25 continuation.resumeWith(result) 26 } 27 } 28 } 29 Dispatchers.Unconfined -> continuation.resumeWith(result) class DispatchedContinuation<T>( 1 override val context: CoroutineContext, 2 val continuation: Continuation<T> 3 ) : Continuation<T> { 4 override fun resumeWith(result: Result<T>) { 5 when (context as Dispatcher) { 6 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 7 continuation.resumeWith(result) 8 } 9 10 Dispatchers.Default -> defaultThreadPool.submit { 11 continuation.resumeWith(result) 12 } 13 14 Dispatchers.IO -> ioThreadPool.submit { 15 continuation.resumeWith(result) 16 } 17 18 19 20 Dispatchers.MainImmediate -> { 21 if(Looper.getMainLooper() == Looper.myLooper()){ 22 continuation.resumeWith(result) 23 }else{ 24 Handler(Looper.getMainLooper()).post { 25 continuation.resumeWith(result) 26 } 27 } 28 } 29 Dispatchers.MainImmediate -> { if(Looper.getMainLooper() == Looper.myLooper()){ continuation.resumeWith(result) }else{ Handler(Looper.getMainLooper()).post { continuation.resumeWith(result) } } } ) : Continuation<T> { 4 override fun resumeWith(result: Result<T>) { 5 when (context as Dispatcher) { 6 Dispatchers.Main -> Handler(Looper.getMainLooper()).post { 7 continuation.resumeWith(result) 8 } 9 10 Dispatchers.Default -> defaultThreadPool.submit { 11 continuation.resumeWith(result) 12 } 13 14 Dispatchers.IO -> ioThreadPool.submit { 15 continuation.resumeWith(result) 16 } 17 18 Dispatchers.Unconfined -> continuation.resumeWith(result) 19 20 21 22 23 24 25 26 27 28 29 } 30 } 31 } 32
  68. GlobalScope.launch { print() } fun main(){ 1 2 // 25314

    3 // 52341 4 // 42315 5 6 runBlocking { print() } 7 // 12345 8 // 12345 9 // 12345 10 } 11 12 suspend fun CoroutineScope.print() { 13 for(x in 1..5){ 14 launch{ 15
  69. GlobalScope.launch { print() } fun main(){ 1 2 // 25314

    3 // 52341 4 // 42315 5 6 runBlocking { print() } 7 // 12345 8 // 12345 9 // 12345 10 } 11 12 suspend fun CoroutineScope.print() { 13 for(x in 1..5){ 14 launch{ 15 runBlocking { print() } fun main(){ 1 GlobalScope.launch { print() } 2 // 25314 3 // 52341 4 // 42315 5 6 7 // 12345 8 // 12345 9 // 12345 10 } 11 12 suspend fun CoroutineScope.print() { 13 for(x in 1..5){ 14 launch{ 15
  70. suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) {

    1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14
  71. suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) {

    1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14
  72. suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) {

    1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14
  73. suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) {

    1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 val callback = object : Continuation<Unit> { override val context = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) { DispatchedContinuation(cont.context, cont).resumeWith(result) } } suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 3 4 5 6 7 8 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14
  74. suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) {

    1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 val callback = object : Continuation<Unit> { override val context = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) { DispatchedContinuation(cont.context, cont).resumeWith(result) } } suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 3 4 5 6 7 8 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 val coroutine = block.createCoroutineUnintercepted(callback) suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14
  75. suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) {

    1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 val callback = object : Continuation<Unit> { override val context = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) { DispatchedContinuation(cont.context, cont).resumeWith(result) } } suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 3 4 5 6 7 8 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 val coroutine = block.createCoroutineUnintercepted(callback) suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 11 COROUTINE_SUSPENDED 12 } 13 } 14
  76. suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) {

    1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 val callback = object : Continuation<Unit> { override val context = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) { DispatchedContinuation(cont.context, cont).resumeWith(result) } } suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 3 4 5 6 7 8 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 val coroutine = block.createCoroutineUnintercepted(callback) suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 11 COROUTINE_SUSPENDED 12 } 13 } 14 COROUTINE_SUSPENDED suspend fun withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { 1 suspendCoroutineUninterceptedOrReturn<Unit> { cont -> 2 val callback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 override fun resumeWith(result: Result<Unit>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 12 } 13 } 14
  77. suspend fun <R> withContext(dispatcher: Dispatcher, block: suspend () -> R):

    R 1 return suspendCoroutineUninterceptedOrReturn<R> { cont -> 2 val callback = object : Continuation<R> { 3 override val context = dispatcher 4 5 override fun resumeWith(result: Result<R>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14
  78. suspend fun <R> withContext(dispatcher: Dispatcher, block: suspend () -> R):

    R 1 return suspendCoroutineUninterceptedOrReturn<R> { cont -> 2 val callback = object : Continuation<R> { 3 override val context = dispatcher 4 5 override fun resumeWith(result: Result<R>) { 6 DispatchedContinuation(cont.context, cont).resumeWith(result) 7 } 8 } 9 val coroutine = block.createCoroutineUnintercepted(callback) 10 DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes 11 COROUTINE_SUSPENDED 12 } 13 } 14 suspend fun <R> withContext(dispatcher: Dispatcher, block: suspend () -> R): R return suspendCoroutineUninterceptedOrReturn<R> { cont -> val callback = object : Continuation<R> { override val context = dispatcher override fun resumeWith(result: Result<R>) { DispatchedContinuation(cont.context, cont).resumeWith(result) } } val coroutine = block.createCoroutineUnintercepted(callback) DispatchedContinuation(dispatcher, coroutine).resumeWith(Result.succes COROUTINE_SUSPENDED } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  79. suspend fun <T> suspendCoroutine(block: (Continuation<T>) -> Unit): T { return

    suspendCoroutineUninterceptedOrReturn { cont -> val dispatchedContinuation = DispatchedContinuation(cont.context, c block(dispatchedContinuation) COROUTINE_SUSPENDED } } 1 2 3 4 5 6 7
  80. fun <T> (suspend () -> T).createCoroutine( completion: Continuation<T> ): Continuation<Unit>

    { val coroutine = createCoroutineUnintercepted(completion) return DispatchedContinuation(completion.context,corouti } 1 2 3 4 5 6
  81. coroutines do not declare what thread they run on, they

    declare what dispatcher they run on
  82. withContext(Dispatchers.Default){ suspend fun waitSomeTime() { 1 2 delay(2000L) 3 }

    4 } 5 suspend fun waitSomeTime() { withContext(Dispatchers.Default){ delay(2000L) } } 1 2 3 4 5
  83. fun main() { runBlocking { val timeTaken = measureTimeMillis {

    async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) }.await() } println("async await took $timeTaken ms") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 } 18
  84. fun main() { runBlocking { val timeTaken = measureTimeMillis {

    async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) }.await() } println("async await took $timeTaken ms") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 } 18 runBlocking { fun main() { 1 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18
  85. fun main() { runBlocking { val timeTaken = measureTimeMillis {

    async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) }.await() } println("async await took $timeTaken ms") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 } 18 runBlocking { fun main() { 1 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 val timeTaken = measureTimeMillis { fun main() { 1 runBlocking { 2 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18
  86. fun main() { runBlocking { val timeTaken = measureTimeMillis {

    async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) }.await() } println("async await took $timeTaken ms") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 } 18 runBlocking { fun main() { 1 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 val timeTaken = measureTimeMillis { fun main() { 1 runBlocking { 2 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 5 6 7 8 9 10 11 12 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18
  87. fun main() { runBlocking { val timeTaken = measureTimeMillis {

    async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) }.await() } println("async await took $timeTaken ms") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 } 18 runBlocking { fun main() { 1 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 val timeTaken = measureTimeMillis { fun main() { 1 runBlocking { 2 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 5 6 7 8 9 10 11 12 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18
  88. fun main() { runBlocking { val timeTaken = measureTimeMillis {

    async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) }.await() } println("async await took $timeTaken ms") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 } 18 runBlocking { fun main() { 1 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 val timeTaken = measureTimeMillis { fun main() { 1 runBlocking { 2 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 5 6 7 8 9 10 11 12 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18
  89. fun main() { runBlocking { val timeTaken = measureTimeMillis {

    async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) }.await() } println("async await took $timeTaken ms") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 } 18 runBlocking { fun main() { 1 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 val timeTaken = measureTimeMillis { fun main() { 1 runBlocking { 2 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 5 6 7 8 9 10 11 12 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 launch { delay(4.seconds) } fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 6 7 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18
  90. fun main() { runBlocking { val timeTaken = measureTimeMillis {

    async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) }.await() } println("async await took $timeTaken ms") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 } 18 runBlocking { fun main() { 1 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 val timeTaken = measureTimeMillis { fun main() { 1 runBlocking { 2 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 5 6 7 8 9 10 11 12 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 launch { delay(4.seconds) } fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 6 7 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18
  91. fun main() { runBlocking { val timeTaken = measureTimeMillis {

    async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) }.await() } println("async await took $timeTaken ms") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 } 18 runBlocking { fun main() { 1 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 val timeTaken = measureTimeMillis { fun main() { 1 runBlocking { 2 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 5 6 7 8 9 10 11 12 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 launch { delay(4.seconds) } fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 6 7 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 launch { delay(2.seconds) } fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 10 11 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18
  92. fun main() { runBlocking { val timeTaken = measureTimeMillis {

    async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) }.await() } println("async await took $timeTaken ms") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 } 18 runBlocking { fun main() { 1 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 val timeTaken = measureTimeMillis { fun main() { 1 runBlocking { 2 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 5 6 7 8 9 10 11 12 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 launch { delay(4.seconds) } fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 6 7 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 launch { delay(2.seconds) } fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 10 11 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18
  93. fun main() { runBlocking { val timeTaken = measureTimeMillis {

    async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) }.await() } println("async await took $timeTaken ms") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 } 18 runBlocking { fun main() { 1 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 val timeTaken = measureTimeMillis { fun main() { 1 runBlocking { 2 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 5 6 7 8 9 10 11 12 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 launch { delay(4.seconds) } fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 6 7 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 launch { delay(2.seconds) } fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 10 11 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 }.await() fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18
  94. fun main() { runBlocking { val timeTaken = measureTimeMillis {

    async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) }.await() } println("async await took $timeTaken ms") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 } 18 runBlocking { fun main() { 1 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 val timeTaken = measureTimeMillis { fun main() { 1 runBlocking { 2 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 5 6 7 8 9 10 11 12 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 launch { delay(4.seconds) } fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 6 7 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 launch { delay(2.seconds) } fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 10 11 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 }.await() fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 println("async await took $timeTaken ms") fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 16 } 17 } 18
  95. fun main() { runBlocking { val timeTaken = measureTimeMillis {

    async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) }.await() } println("async await took $timeTaken ms") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 } 18 runBlocking { fun main() { 1 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 val timeTaken = measureTimeMillis { fun main() { 1 runBlocking { 2 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 5 6 7 8 9 10 11 12 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 async(Dispatchers.IO) { fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 launch { delay(4.seconds) } fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 6 7 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 launch { delay(2.seconds) } fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 10 11 12 delay(1.seconds) 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 delay(1.seconds) fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 13 }.await() 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 }.await() fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 14 } 15 println("async await took $timeTaken ms") 16 } 17 } 18 println("async await took $timeTaken ms") fun main() { 1 runBlocking { 2 val timeTaken = measureTimeMillis { 3 async(Dispatchers.IO) { 4 delay(1.seconds) 5 launch { 6 delay(4.seconds) 7 } 8 delay(1.seconds) 9 launch { 10 delay(2.seconds) 11 } 12 delay(1.seconds) 13 }.await() 14 } 15 16 } 17 } 18 fun main() { runBlocking { val timeTaken = measureTimeMillis { async(Dispatchers.IO) { delay(1.seconds) launch { delay(4.seconds) } delay(1.seconds) launch { delay(2.seconds) } delay(1.seconds) }.await() } println("async await took $timeTaken ms") } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
  96. fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { val

    completionCallback = object : Continuation<Unit> { override val context = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) {} } val continuation = block.createCoroutineUnintercepted(completionCallback) DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(U } 1 2 3 4 5 6 7 8 9 10 11
  97. fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { val

    completionCallback = object : Continuation<Unit> { override val context = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) {} } val continuation = block.createCoroutineUnintercepted(completionCallback) DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(U } 1 2 3 4 5 6 7 8 9 10 11 val completionCallback = object : Continuation<Unit> { override val context = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) {} } fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1 2 3 4 5 6 7 val continuation = block.createCoroutineUnintercepted(completionCallback) 8 9 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(U 10 } 11
  98. fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { val

    completionCallback = object : Continuation<Unit> { override val context = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) {} } val continuation = block.createCoroutineUnintercepted(completionCallback) DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(U } 1 2 3 4 5 6 7 8 9 10 11 val completionCallback = object : Continuation<Unit> { override val context = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) {} } fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1 2 3 4 5 6 7 val continuation = block.createCoroutineUnintercepted(completionCallback) 8 9 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(U 10 } 11 override fun resumeWith(result: Result<Unit>) {} fun launch(dispatcher: Dispatchers, block: suspend () -> Unit) { 1 2 val completionCallback = object : Continuation<Unit> { 3 override val context = EmptyCoroutineContext 4 5 } 6 7 val continuation = block.createCoroutineUnintercepted(completionCallback) 8 9 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(U 10 } 11
  99. override fun resumeWith(result: Result<Unit>) { context[Job]?.waitForChildren() } fun launch(context: CoroutineContext,

    block: suspend () -> Unit) { 1 2 val completionCallback = object : Continuation<Unit> { 3 override val context = context 4 5 6 7 } 8 9 val continuation = block.createCoroutineUnintercepted(completionCallback) 10 11 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(U 12 } 13
  100. override fun resumeWith(result: Result<Unit>) { context[Job]?.waitForChildren() } fun launch(context: CoroutineContext,

    block: suspend () -> Unit) { 1 2 val completionCallback = object : Continuation<Unit> { 3 override val context = context 4 5 6 7 } 8 9 val continuation = block.createCoroutineUnintercepted(completionCallback) 10 11 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(U 12 } 13 fun launch(context: CoroutineContext, block: suspend () -> Unit) { 1 2 val completionCallback = object : Continuation<Unit> { 3 override val context = context 4 override fun resumeWith(result: Result<Unit>) { 5 context[Job]?.waitForChildren() 6 } 7 } 8 9 val continuation = block.createCoroutineUnintercepted(completionCallback) 10 11 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(U 12 } 13
  101. class MyAsyncTask : AsyncTask<Void, Void, Void>() { override fun onPreExecute()

    { ... } override fun doInBackground(vararg params: Void?) { for (i in 0..10) { uploader.upload(...) } } override fun onPostExecute(result: Void) { ... } } 1 2 3 4 5 6 7 8 9 10 11 12 13 ... 14 15 task = MyAsyncTask() 16 task.execute() 17 18 ... 19 20 task.cancel() 21
  102. class MyAsyncTask : AsyncTask<Void, Void, Void>() { override fun onPreExecute()

    { ... } override fun doInBackground(vararg params: Void?) { for (i in 0..10) { uploader.upload(...) } } override fun onPostExecute(result: Void) { ... } } 1 2 3 4 5 6 7 8 9 10 11 12 13 ... 14 15 task = MyAsyncTask() 16 task.execute() 17 18 ... 19 20 task.cancel() 21 task = MyAsyncTask() class MyAsyncTask : AsyncTask<Void, Void, Void>() { 1 2 override fun onPreExecute() { ... } 3 4 override fun doInBackground(vararg params: Void?) { 5 for (i in 0..10) { 6 uploader.upload(...) 7 } 8 } 9 10 override fun onPostExecute(result: Void) { ... } 11 } 12 13 ... 14 15 16 task.execute() 17 18 ... 19 20 task.cancel() 21
  103. class MyAsyncTask : AsyncTask<Void, Void, Void>() { override fun onPreExecute()

    { ... } override fun doInBackground(vararg params: Void?) { for (i in 0..10) { uploader.upload(...) } } override fun onPostExecute(result: Void) { ... } } 1 2 3 4 5 6 7 8 9 10 11 12 13 ... 14 15 task = MyAsyncTask() 16 task.execute() 17 18 ... 19 20 task.cancel() 21 task = MyAsyncTask() class MyAsyncTask : AsyncTask<Void, Void, Void>() { 1 2 override fun onPreExecute() { ... } 3 4 override fun doInBackground(vararg params: Void?) { 5 for (i in 0..10) { 6 uploader.upload(...) 7 } 8 } 9 10 override fun onPostExecute(result: Void) { ... } 11 } 12 13 ... 14 15 16 task.execute() 17 18 ... 19 20 task.cancel() 21 task.execute() class MyAsyncTask : AsyncTask<Void, Void, Void>() { 1 2 override fun onPreExecute() { ... } 3 4 override fun doInBackground(vararg params: Void?) { 5 for (i in 0..10) { 6 uploader.upload(...) 7 } 8 } 9 10 override fun onPostExecute(result: Void) { ... } 11 } 12 13 ... 14 15 task = MyAsyncTask() 16 17 18 ... 19 20 task.cancel() 21
  104. class MyAsyncTask : AsyncTask<Void, Void, Void>() { override fun onPreExecute()

    { ... } override fun doInBackground(vararg params: Void?) { for (i in 0..10) { uploader.upload(...) } } override fun onPostExecute(result: Void) { ... } } 1 2 3 4 5 6 7 8 9 10 11 12 13 ... 14 15 task = MyAsyncTask() 16 task.execute() 17 18 ... 19 20 task.cancel() 21 task = MyAsyncTask() class MyAsyncTask : AsyncTask<Void, Void, Void>() { 1 2 override fun onPreExecute() { ... } 3 4 override fun doInBackground(vararg params: Void?) { 5 for (i in 0..10) { 6 uploader.upload(...) 7 } 8 } 9 10 override fun onPostExecute(result: Void) { ... } 11 } 12 13 ... 14 15 16 task.execute() 17 18 ... 19 20 task.cancel() 21 task.execute() class MyAsyncTask : AsyncTask<Void, Void, Void>() { 1 2 override fun onPreExecute() { ... } 3 4 override fun doInBackground(vararg params: Void?) { 5 for (i in 0..10) { 6 uploader.upload(...) 7 } 8 } 9 10 override fun onPostExecute(result: Void) { ... } 11 } 12 13 ... 14 15 task = MyAsyncTask() 16 17 18 ... 19 20 task.cancel() 21 task.cancel() class MyAsyncTask : AsyncTask<Void, Void, Void>() { 1 2 override fun onPreExecute() { ... } 3 4 override fun doInBackground(vararg params: Void?) { 5 for (i in 0..10) { 6 uploader.upload(...) 7 } 8 } 9 10 override fun onPostExecute(result: Void) { ... } 11 } 12 13 ... 14 15 task = MyAsyncTask() 16 task.execute() 17 18 ... 19 20 21
  105. class MyAsyncTask : AsyncTask<Void, Void, Void>() { override fun onPreExecute()

    { ... } override fun doInBackground(vararg params: Void?) { for (i in 0..10) { uploader.upload(...) } } override fun onPostExecute(result: Void) { ... } } 1 2 3 4 5 6 7 8 9 10 11 12 13 ... 14 15 task = MyAsyncTask() 16 task.execute() 17 18 ... 19 20 task.cancel() 21 task = MyAsyncTask() class MyAsyncTask : AsyncTask<Void, Void, Void>() { 1 2 override fun onPreExecute() { ... } 3 4 override fun doInBackground(vararg params: Void?) { 5 for (i in 0..10) { 6 uploader.upload(...) 7 } 8 } 9 10 override fun onPostExecute(result: Void) { ... } 11 } 12 13 ... 14 15 16 task.execute() 17 18 ... 19 20 task.cancel() 21 task.execute() class MyAsyncTask : AsyncTask<Void, Void, Void>() { 1 2 override fun onPreExecute() { ... } 3 4 override fun doInBackground(vararg params: Void?) { 5 for (i in 0..10) { 6 uploader.upload(...) 7 } 8 } 9 10 override fun onPostExecute(result: Void) { ... } 11 } 12 13 ... 14 15 task = MyAsyncTask() 16 17 18 ... 19 20 task.cancel() 21 task.cancel() class MyAsyncTask : AsyncTask<Void, Void, Void>() { 1 2 override fun onPreExecute() { ... } 3 4 override fun doInBackground(vararg params: Void?) { 5 for (i in 0..10) { 6 uploader.upload(...) 7 } 8 } 9 10 override fun onPostExecute(result: Void) { ... } 11 } 12 13 ... 14 15 task = MyAsyncTask() 16 task.execute() 17 18 ... 19 20 21 class MyAsyncTask : AsyncTask<Void, Void, Void>() { override fun onPreExecute() { ... } override fun doInBackground(vararg params: Void?) { for (i in 0..10) { uploader.upload(...) } } override fun onPostExecute(result: Void) { ... } } 1 2 3 4 5 6 7 8 9 10 11 12 13 ... 14 15 task = MyAsyncTask() 16 task.execute() 17 18 ... 19 20 task.cancel() 21
  106. if(isCancelled){ return } class MyAsyncTask : AsyncTask<Void, Void, Void>() {

    1 2 override fun onPreExecute() { ... } 3 4 override fun doInBackground(vararg params: Void?) { 5 for (i in 0..10) { 6 7 uploader.upload(...) 8 } 9 } 10 11 override fun onPostExecute(result: Void) { ... } 12 } 13 14 ... 15 16 task = MyAsyncTask() 17 task.execute() 18 19 ... 20 21 task.cancel() 22
  107. interface CoroutineScope{ val context: CoroutineContext fun cancel(){ context[Job]?.cancel() ?: error("Cannot

    cancel scope without job!") } } object GlobalScope: CoroutineScope{ override val context: CoroutineContext = EmptyCoroutineContext } val viewModelScope = object: CoroutineScope{ override val context: CoroutineContext = Dispatchers.Main + Job() } 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  108. fun CoroutineScope.launch(extraContext: CoroutineContext, block: suspend CoroutineSc 1 2 val completionCallback

    = object : Continuation<Unit>, CoroutineScope, Job { 3 init{ 4 context[Job]?.addChildren(this) 5 } 6 override val context = scopeContext + extraContext + this 7 override fun resumeWith(result: Result<Unit>) { 8 context[Job]?.waitForChildren() 9 } 10 } 11 12 val continuation = block.createCoroutineUnintercepted(completionCallback,complet 13 14 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(Unit)) 15 } 16
  109. fun CoroutineScope.launch(extraContext: CoroutineContext, block: suspend CoroutineSc 1 2 val completionCallback

    = object : Continuation<Unit>, CoroutineScope, Job { 3 init{ 4 context[Job]?.addChildren(this) 5 } 6 override val context = scopeContext + extraContext + this 7 override fun resumeWith(result: Result<Unit>) { 8 context[Job]?.waitForChildren() 9 } 10 } 11 12 val continuation = block.createCoroutineUnintercepted(completionCallback,complet 13 14 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(Unit)) 15 } 16 val completionCallback = object : Continuation<Unit>, CoroutineScope, Job { fun CoroutineScope.launch(extraContext: CoroutineContext, block: suspend CoroutineSc 1 2 3 init{ 4 context[Job]?.addChildren(this) 5 } 6 override val context = scopeContext + extraContext + this 7 override fun resumeWith(result: Result<Unit>) { 8 context[Job]?.waitForChildren() 9 } 10 } 11 12 val continuation = block.createCoroutineUnintercepted(completionCallback,complet 13 14 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(Unit)) 15 } 16
  110. fun CoroutineScope.launch(extraContext: CoroutineContext, block: suspend CoroutineSc 1 2 val completionCallback

    = object : Continuation<Unit>, CoroutineScope, Job { 3 init{ 4 context[Job]?.addChildren(this) 5 } 6 override val context = scopeContext + extraContext + this 7 override fun resumeWith(result: Result<Unit>) { 8 context[Job]?.waitForChildren() 9 } 10 } 11 12 val continuation = block.createCoroutineUnintercepted(completionCallback,complet 13 14 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(Unit)) 15 } 16 val completionCallback = object : Continuation<Unit>, CoroutineScope, Job { fun CoroutineScope.launch(extraContext: CoroutineContext, block: suspend CoroutineSc 1 2 3 init{ 4 context[Job]?.addChildren(this) 5 } 6 override val context = scopeContext + extraContext + this 7 override fun resumeWith(result: Result<Unit>) { 8 context[Job]?.waitForChildren() 9 } 10 } 11 12 val continuation = block.createCoroutineUnintercepted(completionCallback,complet 13 14 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(Unit)) 15 } 16 init{ context[Job]?.addChildren(this) } fun CoroutineScope.launch(extraContext: CoroutineContext, block: suspend CoroutineSc 1 2 val completionCallback = object : Continuation<Unit>, CoroutineScope, Job { 3 4 5 6 override val context = scopeContext + extraContext + this 7 override fun resumeWith(result: Result<Unit>) { 8 context[Job]?.waitForChildren() 9 } 10 } 11 12 val continuation = block.createCoroutineUnintercepted(completionCallback,complet 13 14 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(Unit)) 15 } 16
  111. fun CoroutineScope.launch(extraContext: CoroutineContext, block: suspend CoroutineSc 1 2 val completionCallback

    = object : Continuation<Unit>, CoroutineScope, Job { 3 init{ 4 context[Job]?.addChildren(this) 5 } 6 override val context = scopeContext + extraContext + this 7 override fun resumeWith(result: Result<Unit>) { 8 context[Job]?.waitForChildren() 9 } 10 } 11 12 val continuation = block.createCoroutineUnintercepted(completionCallback,complet 13 14 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(Unit)) 15 } 16 val completionCallback = object : Continuation<Unit>, CoroutineScope, Job { fun CoroutineScope.launch(extraContext: CoroutineContext, block: suspend CoroutineSc 1 2 3 init{ 4 context[Job]?.addChildren(this) 5 } 6 override val context = scopeContext + extraContext + this 7 override fun resumeWith(result: Result<Unit>) { 8 context[Job]?.waitForChildren() 9 } 10 } 11 12 val continuation = block.createCoroutineUnintercepted(completionCallback,complet 13 14 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(Unit)) 15 } 16 init{ context[Job]?.addChildren(this) } fun CoroutineScope.launch(extraContext: CoroutineContext, block: suspend CoroutineSc 1 2 val completionCallback = object : Continuation<Unit>, CoroutineScope, Job { 3 4 5 6 override val context = scopeContext + extraContext + this 7 override fun resumeWith(result: Result<Unit>) { 8 context[Job]?.waitForChildren() 9 } 10 } 11 12 val continuation = block.createCoroutineUnintercepted(completionCallback,complet 13 14 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(Unit)) 15 } 16 context[Job]?.waitForChildren() fun CoroutineScope.launch(extraContext: CoroutineContext, block: suspend CoroutineSc 1 2 val completionCallback = object : Continuation<Unit>, CoroutineScope, Job { 3 init{ 4 context[Job]?.addChildren(this) 5 } 6 override val context = scopeContext + extraContext + this 7 override fun resumeWith(result: Result<Unit>) { 8 9 } 10 } 11 12 val continuation = block.createCoroutineUnintercepted(completionCallback,complet 13 14 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(Unit)) 15 } 16
  112. fun CoroutineScope.launch(extraContext: CoroutineContext, block: suspend CoroutineSc 1 2 val completionCallback

    = object : Continuation<Unit>, CoroutineScope, Job { 3 init{ 4 context[Job]?.addChildren(this) 5 } 6 override val context = scopeContext + extraContext + this 7 override fun resumeWith(result: Result<Unit>) { 8 context[Job]?.waitForChildren() 9 } 10 } 11 12 val continuation = block.createCoroutineUnintercepted(completionCallback,complet 13 14 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(Unit)) 15 } 16 val completionCallback = object : Continuation<Unit>, CoroutineScope, Job { fun CoroutineScope.launch(extraContext: CoroutineContext, block: suspend CoroutineSc 1 2 3 init{ 4 context[Job]?.addChildren(this) 5 } 6 override val context = scopeContext + extraContext + this 7 override fun resumeWith(result: Result<Unit>) { 8 context[Job]?.waitForChildren() 9 } 10 } 11 12 val continuation = block.createCoroutineUnintercepted(completionCallback,complet 13 14 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(Unit)) 15 } 16 init{ context[Job]?.addChildren(this) } fun CoroutineScope.launch(extraContext: CoroutineContext, block: suspend CoroutineSc 1 2 val completionCallback = object : Continuation<Unit>, CoroutineScope, Job { 3 4 5 6 override val context = scopeContext + extraContext + this 7 override fun resumeWith(result: Result<Unit>) { 8 context[Job]?.waitForChildren() 9 } 10 } 11 12 val continuation = block.createCoroutineUnintercepted(completionCallback,complet 13 14 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(Unit)) 15 } 16 context[Job]?.waitForChildren() fun CoroutineScope.launch(extraContext: CoroutineContext, block: suspend CoroutineSc 1 2 val completionCallback = object : Continuation<Unit>, CoroutineScope, Job { 3 init{ 4 context[Job]?.addChildren(this) 5 } 6 override val context = scopeContext + extraContext + this 7 override fun resumeWith(result: Result<Unit>) { 8 9 } 10 } 11 12 val continuation = block.createCoroutineUnintercepted(completionCallback,complet 13 14 DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(Unit)) 15 } 16 fun CoroutineScope.launch(extraContext: CoroutineContext, block: suspend CoroutineSc val completionCallback = object : Continuation<Unit>, CoroutineScope, Job { init{ context[Job]?.addChildren(this) } override val context = scopeContext + extraContext + this override fun resumeWith(result: Result<Unit>) { context[Job]?.waitForChildren() } } val continuation = block.createCoroutineUnintercepted(completionCallback,complet DispatchedContinuation(dispatcher,continuation).resumeWith(Result.success(Unit)) } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  113. IS IT THAT SIMPLE? Things we did not consider Race

    conditions, Deadlocks Multi language support
  114. IS IT THAT SIMPLE? Things we did not consider Race

    conditions, Deadlocks Multi language support Edge cases
  115. IS IT THAT SIMPLE? Things we did not consider Race

    conditions, Deadlocks Multi language support Edge cases Performance optimizations
  116. IS IT THAT SIMPLE? Things we did not consider Race

    conditions, Deadlocks Multi language support Edge cases Performance optimizations Flows, sequences, channels, actors, Mutexes, selects
  117. IS IT THAT SIMPLE? Things we did not consider Race

    conditions, Deadlocks Multi language support Edge cases Performance optimizations Flows, sequences, channels, actors, Mutexes, selects Exception handling