Slide 1

Slide 1 text

Confidential Kotlin Coroutines in-depth analysis Version 1.0 Kotlin Coroutines In-depth analysis of Kotlin coroutines

Slide 2

Slide 2 text

Confidential Kotlin Coroutines in-depth analysis Version 1.0 Rostyslav Lesovyi

Slide 3

Slide 3 text

Confidential Kotlin Coroutines in-depth analysis // Welcome

Slide 4

Slide 4 text

Confidential Kotlin Coroutines in-depth analysis DB ThreadPool Cloud ThreadPool DB Cloud // Welcome MemoryApi PersistentApi CloudApi Api

Slide 5

Slide 5 text

Confidential Kotlin Coroutines in-depth analysis DB ThreadPool Cloud ThreadPool DB Cloud // Welcome to Async Hell MemoryApi PersistentApi CloudApi Api

Slide 6

Slide 6 text

Confidential Kotlin Coroutines in-depth analysis // Welcome to Async Hell 1. abstract fun put(id: String, user: User) 2. abstract fun getMemory(id: String): User? 3. abstract fun getPersistent(id: String): User? 4. abstract fun getCloud(id: String): User? 5. 6. fun get(id: String, callback: (User?) -> Unit) = 7. object : AsyncTask() { 8. override fun doInBackground(vararg params: String): User? { 9. val user = getMemory(params[0]) 10. ?: getPersistent(params[0]) 11. ?: getCloud(params[0]) 12. ?: return null 13. 14. put(id, user) 15. return user 16. } 17. 18. override fun onPostExecute(result: User?) = callback(result) 19. }.execute(id)

Slide 7

Slide 7 text

Confidential Kotlin Coroutines in-depth analysis // Welcome to Async Hell 1. fun put(id: String, user: User, callback: () -> Unit) 2. fun getMemory(id: String, callback: (User?) -> Unit) 3. fun getPersistent(id: String, callback: (User?) -> Unit) 4. fun getCloud(id: String, callback: (User?) -> Unit) 5. 6. fun getData(id: String, callback: (User?) -> Unit) { 7. getMemory(id) { memoryData -> 8. if (memoryData != null) return@getMemory callback(memoryData) 9. getPersistent(id) { dbData -> 10. if (dbData != null) { 11. put(id, dbData) { return@put callback(dbData) } 12. } 13. getCloud(id) { cloudData -> 14. if (cloudData != null) { 15. put(id, cloudData) { return@put callback(cloudData) } 16. } else { 17. return@getCloud callback(null) 18. } 19. }}}} // yeah... code doesn't fit presentation screen

Slide 8

Slide 8 text

Confidential Kotlin Coroutines in-depth analysis // Welcome to Async Hell 1. fun put(id: String, user: User): Completable 2. fun getMemory(id: String): Maybe 3. fun getPersistent(id: String): Maybe 4. fun getCloud(id: String): Maybe 5. 6. fun get(id: String) = getMemory(id) 7. .switchIfEmpty(getPersistent(id)) 8. .switchIfEmpty(getCloud(id)) 9. .flatMap { user -> 10. put(id, user).andThen(Maybe.just(user)) 11. }

Slide 9

Slide 9 text

Confidential Kotlin Coroutines in-depth analysis // Coroutines to the rescue! 1. suspend fun put(id: String, user: User) 2. suspend fun getMemory(id: String): User? 3. suspend fun getPersistent(id: String): User? 4. suspend fun getCloud(id: String): User? 5. 6. suspend fun get(id: String): User? { 7. val user = getMemory(id) // executes immediately 8. ?: getPersistent(id) // runs on DB thread pool 9. ?: getCloud(id) // runs on network thread pool 10. ?: return null 11. 12. put(id, user) // runs on DB thread pool 13. return user 14. }

Slide 10

Slide 10 text

Confidential Kotlin Coroutines in-depth analysis // Some Theory

Slide 11

Slide 11 text

Confidential Kotlin Coroutines in-depth analysis Thread 1 // What is Singlethreading? Task 1 Task 2 Task 3 Task 4

Slide 12

Slide 12 text

Confidential Kotlin Coroutines in-depth analysis Thread 1 // What is Multithreading? Thread 2 Task 1 Task 3 Task 2 Task 4

Slide 13

Slide 13 text

Confidential Kotlin Coroutines in-depth analysis Thread 1 // What is Coroutine? Task 1 Task 3 Thread 2 Task 2 Task 4 Result

Slide 14

Slide 14 text

Confidential Kotlin Coroutines in-depth analysis // What are Coroutines Coroutine Scope Coroutine Coroutine 1 Coroutine Context Job Dispatcher ... Coroutine N

Slide 15

Slide 15 text

Confidential Kotlin Coroutines in-depth analysis // What are Coroutines Coroutine Scope Coroutine Coroutine 1 Coroutine Context Job Dispatcher ... Coroutine N

Slide 16

Slide 16 text

Confidential Kotlin Coroutines in-depth analysis // What are Coroutines “Coroutines are light-weight threads” Kotlin Doc

Slide 17

Slide 17 text

Confidential Kotlin Coroutines in-depth analysis // What are Coroutines “Coroutines are executors with fancy syntax” Me

Slide 18

Slide 18 text

Confidential Kotlin Coroutines in-depth analysis // What are Coroutines 1. fun foo() = executor.execute { 2. // do something 3. } 1. suspend fun bar() = withContext(Dispatchers.Default) { 2. // do something 3. }

Slide 19

Slide 19 text

Confidential Kotlin Coroutines in-depth analysis // What are Coroutines 1. fun foo() = executor1.execute { 2. // do something on executor1 3. executor2.execute { 4. // do something on executor2 5. executor1.execute { /* do something on executor1 */ } 6. } 7. } 1. suspend fun bar() = withContext(Dispatchers.Default) { 2. // do something 3. }

Slide 20

Slide 20 text

Confidential Kotlin Coroutines in-depth analysis // What are Coroutines 1. fun foo() = executor1.execute { 2. // do something on executor1 3. executor2.execute { 4. // do something on executor2 5. executor1.execute { /* do something on executor1 */ } 6. } 7. } 1. suspend fun bar() = withContext(Dispatchers.Default) { 2. // do something on default dispatcher 3. withContext(Dispatchers.IO) { 4. // do something on IO dispatcher 5. } 6. // do something on default dispatcher 7. }

Slide 21

Slide 21 text

Confidential Kotlin Coroutines in-depth analysis // What are Coroutines 1. fun foo(callback: (String) -> Unit) = executor1.execute { 2. // do something on executor1 3. executor2.execute { 4. // do something on executor2 5. executor1.execute { callback("result") } 6. } 7. } 1. suspend fun bar() = withContext(Dispatchers.Default) { 2. // do something on default dispatcher 3. withContext(Dispatchers.IO) { 4. // do something on IO dispatcher 5. } 6. // do something on default dispatcher 7. }

Slide 22

Slide 22 text

Confidential Kotlin Coroutines in-depth analysis // What are Coroutines 1. fun foo(callback: (String) -> Unit) = executor1.execute { 2. // do something on executor1 3. executor2.execute { 4. // do something on executor2 5. executor1.execute { callback("result") } 6. } 7. } 1. suspend fun bar() = withContext(Dispatchers.Default) { 2. // do something on default dispatcher 3. withContext(Dispatchers.IO) { 4. // do something on IO dispatcher 5. } 6. // do something on default dispatcher 7. return@withContext "result" 8. }

Slide 23

Slide 23 text

Confidential Kotlin Coroutines in-depth analysis // How do Coroutines work? 1. fun foo() { 2. 3. } 4. 5. suspend fun bar() { 6. }

Slide 24

Slide 24 text

Confidential Kotlin Coroutines in-depth analysis // How do Coroutines work? 1. fun foo() { 2. bar() 3. } 4. 5. suspend fun bar() { 6. } suspend function should be called only from a coroutine or another suspend function

Slide 25

Slide 25 text

Confidential Kotlin Coroutines in-depth analysis // How do Coroutines work? 1. fun foo() { 2. GlobalScope.async { 3. bar() 4. } 5. } 6. 7. suspend fun bar() { 8. }

Slide 26

Slide 26 text

Confidential Kotlin Coroutines in-depth analysis // How do Coroutines work? 1. fun foo() { 2. GlobalScope.async { 3. bar() 4. } 5. } 6. 7. suspend fun bar() { 8. // before 9. withContext(Dispatchers.IO) { 10. // hard work 11. } 12. // after 13. } Caller Thread Dispatcher.Default Dispatcher.IO Dispatcher.Default Dispatcher.Default Caller Thread

Slide 27

Slide 27 text

Confidential Kotlin Coroutines in-depth analysis // How do Coroutines work?

Slide 28

Slide 28 text

Confidential Kotlin Coroutines in-depth analysis // How do Coroutines work? 1. suspend fun foo() { 2. var text = "before" 3. bar() 4. text = "after" 5. } 1. fun foo(continuation: Continuation<*>) { 2. val exception = continuation.exception 3. when (continuation.label) { 4. 0 -> { 5. if (exception != null) throw exception 6. var text = "before" 7. continuation.L$0 = text 8. continuation.label = 1 9. if (bar() == SUSPENDED) return SUSPENDED 10. } 11. 1 -> { 12. var text = continuation.L$0 13. if (exception != null) throw exception 14. } 15. } 16. var text = "after" 17. }

Slide 29

Slide 29 text

Confidential Kotlin Coroutines in-depth analysis // How do Coroutines work? Coroutine Scope Coroutine Coroutine 1 Coroutine Context Job Dispatcher ... ... Coroutine N Coroutine 1 Coroutine 2 ... Coroutine N

Slide 30

Slide 30 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine lifecycle Coroutine Scope Coroutine Coroutine 1 Coroutine Context Job Dispatcher ... Coroutine N

Slide 31

Slide 31 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine lifecycle - Job 1. val job = GlobalScope.launch { 2. // do some stuff 3. }

Slide 32

Slide 32 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine lifecycle - Job 1. val job = GlobalScope.launch { 2. // do some stuff 3. } 4. 5. job.invokeOnCompletion { cause -> 6. // job has finished 7. assert(cause == null) 8. assert(job.isCompleted) 9. }

Slide 33

Slide 33 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine lifecycle - Lazy Job 1. val job = GlobalScope.launch(start = CoroutineStart.LAZY) { 2. // do some stuff 3. }

Slide 34

Slide 34 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine lifecycle - Lazy Job 1. val job = GlobalScope.launch(start = CoroutineStart.LAZY) { 2. // do some stuff 3. } 4. 5. job.invokeOnCompletion { cause -> 6. // job has finished 7. assert(cause == null) 8. assert(job.isCompleted) 9. }

Slide 35

Slide 35 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine lifecycle - Lazy Job 1. val job = GlobalScope.launch(start = CoroutineStart.LAZY) { 2. // do some stuff 3. } 4. 5. job.invokeOnCompletion { cause -> 6. // job has finished 7. assert(cause == null) 8. assert(job.isCompleted) 9. } 10. 11. assert(!job.isActive) // new state 12. job.start() 13. assert(job.isActive) // active state

Slide 36

Slide 36 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine lifecycle - Deferred 1. val deferred = GlobalScope.async { 2. // do some stuff 3. return@async "result" 4. }

Slide 37

Slide 37 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine lifecycle - Deferred 1. val deferred = GlobalScope.async { 2. // do some stuff 3. return@async "result" 4. } 5. 6. deferred.invokeOnCompletion { cause -> 7. // job has finished, we can get the result 8. assert(cause == null) 9. assert(job.isCompleted) 10. assert(deferred.getCompleted() == "result") 11. }

Slide 38

Slide 38 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine lifecycle - Error 1. val job = GlobalScope.launch { 2. // oops 3. throw IndexOutOfBoundsException() 4. }

Slide 39

Slide 39 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine lifecycle - Error 1. val job = GlobalScope.launch { 2. // oops 3. throw IndexOutOfBoundsException() 4. } 5. 6. job.invokeOnCompletion { cause -> 7. // job has finished with exception 8. assert(job.isCancelled) 9. assert(job.isCompleted) 10. assert(cause is IndexOutOfBoundsException) 11. }

Slide 40

Slide 40 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine lifecycle - Cancel 1. val job = GlobalScope.launch { 2. // long operation 3. delay(5000) 4. }

Slide 41

Slide 41 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine lifecycle - Cancel 1. val job = GlobalScope.launch { 2. // long operation 3. delay(5000) 4. } 5. 6. job.cancel()

Slide 42

Slide 42 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine lifecycle - Cancel 1. val job = GlobalScope.launch { 2. // long operation 3. delay(5000) 4. } 5. 6. job.cancel() 7. 8. job.invokeOnCompletion { cause -> 9. // job has finished with exception 10. assert(job.isCancelled) 11. assert(job.isCompleted) 12. assert(cause is CancellationException) 13. }

Slide 43

Slide 43 text

Confidential Kotlin Coroutines in-depth analysis Error flow Normal flow // Coroutine lifecycle New Active Completing Completed Cancelling Cancelled running waiting for children exception / cancel

Slide 44

Slide 44 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine lifecycle States isActive isCompleted isCancelled New false false false Active true false false Completing true false false Cancelling false false true Cancelled false true true Completed false true false

Slide 45

Slide 45 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine context Coroutine Scope Coroutine Coroutine 1 Coroutine Context Job Dispatcher ... Coroutine N

Slide 46

Slide 46 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine context Coroutine context includes: ● Main ○ Dispatcher { Executors } ○ Job { Future } ● Additional ○ ThreadContextElement ○ ContinuationInterceptor ○ Default exception handlers { Thread.setUncaughtExceptionHandler } ● Custom ○ Any number of custom values { ThreadLocal }

Slide 47

Slide 47 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine context 1. GlobalScope.launch(Dispatchers.IO) { 2. // IO thread pool 3. }

Slide 48

Slide 48 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine context 1. GlobalScope.launch(Dispatchers.IO) { 2. // IO thread pool 3. withContext(Dispatchers.Main) { 4. // Main thread pool 5. } 6. // IO thread pool 7. }

Slide 49

Slide 49 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine context 1. class MyData(val value: String) : AbstractCoroutineContextElement(Key) { 2. object Key : CoroutineContext.Key 3. }

Slide 50

Slide 50 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine context 1. class MyData(val value: String) : AbstractCoroutineContextElement(Key) { 2. object Key : CoroutineContext.Key 3. } 4. 5. fun foo() { 6. GlobalScope.launch(Dispatchers.IO + MyData("test")) { 7. 8. 9. } 10. }

Slide 51

Slide 51 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine context 1. class MyData(val value: String) : AbstractCoroutineContextElement(Key) { 2. object Key : CoroutineContext.Key 3. } 4. 5. fun foo() { 6. GlobalScope.launch(Dispatchers.IO + MyData("test")) { 7. val data = coroutineContext[MyData.Key] as MyData 8. assert(data.value == "test") 9. } 10. }

Slide 52

Slide 52 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine context 1. class MyData(val value: String) : AbstractCoroutineContextElement(Key) { 2. object Key : CoroutineContext.Key 3. } 4. 5. fun foo() { 6. GlobalScope.launch(Dispatchers.IO) { 7. assert(coroutineContext[MyData.Key] == null) 8. 9. withContext(MyData("test")) { 10. assert(coroutineContext[MyData.Key] != null) 11. } 12. 13. assert(coroutineContext[MyData.Key] == null) 14. } 15. }

Slide 53

Slide 53 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine context [Example] 1. class Benchmark : AbstractCoroutineContextElement(Key) { 2. object Key : CoroutineContext.Key 3. 4. val map = ConcurrentHashMap() 5. }

Slide 54

Slide 54 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine context [Example] 1. class Benchmark : AbstractCoroutineContextElement(Key) { 2. object Key : CoroutineContext.Key 3. 4. val map = ConcurrentHashMap() 5. } 6. 7. suspend inline fun benchmark(name: String, block: () -> T): T { 8. val time = System.currentTimeMillis() 9. try { 10. return block() 11. } finally { 12. (coroutineContext[Benchmark.Key] as Benchmark).map 13. .getOrPut(name) { AtomicLong() } 14. .addAndGet(System.currentTimeMillis() - time) 15. } 16. }

Slide 55

Slide 55 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine context [Example] 1. suspend fun fetchData() = benchmark("fetchData") { 2. return@benchmark "result" 3. }

Slide 56

Slide 56 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine context [Example] 1. suspend fun fetchData() = benchmark("fetchData") { 2. return@benchmark "result" 3. } 4. 5. fun main() { 6. val benchmark = Benchmark() 7. 8. GlobalScope.launch(benchmark) { 9. fetchData() 10. } 11. }

Slide 57

Slide 57 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine context [Example] 1. suspend fun fetchData() = benchmark("fetchData") { 2. return@benchmark "result" 3. } 4. 5. fun main() { 6. val benchmark = Benchmark() 7. 8. GlobalScope.launch(benchmark) { 9. fetchData() 10. 11. // log benchmarks 12. System.out.println(benchmark.map) 13. } 14. }

Slide 58

Slide 58 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine context [Example] 02-27 12:17:44.402 ? D/Benchmark: benchmark results: CmsManagerCentral.Epg_FetchFavoriteChannels 90.868 ms EpgChannelsApi_Combined.get 0.071 ms EpgChannelsApi.get 0.005 ms EpgFavoritesApi_Combined.fetch 85.428 ms EpgFavoritesApi_Cms.fetch 73.104 ms EpgFavoritesApi_Memory.fetch 0.016 ms EpgFavoritesApi_Persistent.fetch 2.870 ms EpgFavoritesApi_Persistent.fetch (inner) 2.865 ms

Slide 59

Slide 59 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine dispatchers Coroutine Scope Coroutine Coroutine 1 Coroutine Context Job Dispatcher ... Coroutine N

Slide 60

Slide 60 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine dispatchers 1. GlobalScope.launch(Dispatchers.IO) { 2. // IO thread pool 3. withContext(Dispatchers.Main) { 4. // Main thread pool 5. } 6. } 1. ioExecutor.execute { 2. // IO thread pool 3. mainExecutor.execute { 4. // Main thread pool 5. } 6. }

Slide 61

Slide 61 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine dispatchers 1. abstract class CoroutineDispatcher : ContinuationInterceptor { 2. /** 3. * Returns `true` if execution shall be dispatched onto another thread. 4. */ 5. open fun isDispatchNeeded(context: CoroutineContext) = true 6. 7. /** 8. * Dispatches execution of a runnable [block] onto another thread in the given [context]. 9. */ 10. abstract fun dispatch(context: CoroutineContext, block: Runnable) 11. }

Slide 62

Slide 62 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine dispatchers [Example] 1. object MainThreadDispatcher : CoroutineDispatcher() { 2. private val handler = Handler(Looper.getMainLooper()) 3. 4. override fun dispatch(context: CoroutineContext, block: Runnable) { 5. handler.post(block) 6. } 7. }

Slide 63

Slide 63 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine dispatchers [Example] 1. object MainThreadDispatcher : CoroutineDispatcher() { 2. private val handler = Handler(Looper.getMainLooper()) 3. 4. override fun dispatch(context: CoroutineContext, block: Runnable) { 5. handler.post(block) 6. } 7. 8. override fun isDispatchNeeded(context: CoroutineContext): Boolean { 9. return !handler.looper.isCurrentThread() 10. } 11. }

Slide 64

Slide 64 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine dispatchers [Example] 1. object MainThreadDispatcher : CoroutineDispatcher() { 2. private val handler = Handler(Looper.getMainLooper()) 3. 4. override fun dispatch(context: CoroutineContext, block: Runnable) { 5. handler.post(block) 6. } 7. 8. override fun isDispatchNeeded(context: CoroutineContext): Boolean { 9. return !handler.looper.isCurrentThread() 10. } 11. } 12. 13. GlobalScope.launch(MainThreadDispatcher) { 14. // runs on Android's main thread 15. }

Slide 65

Slide 65 text

Confidential Kotlin Coroutines in-depth analysis // Child coroutines Coroutine Scope Coroutine Coroutine 1 Coroutine Context Job Dispatcher ... Coroutine N

Slide 66

Slide 66 text

Confidential Kotlin Coroutines in-depth analysis // Child coroutines - Starting 1. suspend fun slowRandom(max: Long): Long { 2. Thread.sleep(1000) 3. return Random.nextLong(max) 4. }

Slide 67

Slide 67 text

Confidential Kotlin Coroutines in-depth analysis // Child coroutines - Starting 1. suspend fun slowRandom(max: Long): Long { 2. delay(1000) 3. return Random.nextLong(max) 4. }

Slide 68

Slide 68 text

Confidential Kotlin Coroutines in-depth analysis // Child coroutines - Starting 1. suspend fun slowRandom(max: Long): Long { 2. delay(1000) 3. return Random.nextLong(max) 4. } 5. 6. suspend fun combine(): Long { 7. val value1 = slowRandom(100) 8. val value2 = slowRandom(200) 9. return value1 + value2 10. }

Slide 69

Slide 69 text

Confidential Kotlin Coroutines in-depth analysis // Child coroutines - Starting 1. suspend fun slowRandom(max: Long): Long { 2. delay(1000) 3. return Random.nextLong(max) 4. } 5. 6. suspend fun combine(): Long { 7. val value1 = GlobalScope.async { slowRandom(100) } 8. val value2 = GlobalScope.async { slowRandom(200) } 9. return value1.await() + value2.await() 10. }

Slide 70

Slide 70 text

Confidential Kotlin Coroutines in-depth analysis // Child coroutines - Starting 1. suspend fun slowRandom(max: Long): Long { 2. delay(1000) 3. return Random.nextLong(max) 4. } 5. 6. suspend fun combine(): Long { 7. val value1 = GlobalScope.async(coroutineContext) { slowRandom(100) } 8. val value2 = GlobalScope.async(coroutineContext) { slowRandom(200) } 9. return value1.await() + value2.await() 10. }

Slide 71

Slide 71 text

Confidential Kotlin Coroutines in-depth analysis // Child coroutines - Starting 1. suspend fun slowRandom(max: Long): Long { 2. delay(1000) 3. return Random.nextLong(max) 4. } 5. 6. suspend fun combine() = coroutineScope { 7. val value1 = async { slowRandom(100) } 8. val value2 = async { slowRandom(200) } 9. return@coroutineScope value1.await() + value2.await() 10. }

Slide 72

Slide 72 text

Confidential Kotlin Coroutines in-depth analysis // Child coroutines - Starting 1. suspend fun slowRandom(max: Long): Long { 2. delay(1000) 3. return Random.nextLong(max) 4. } 5. 6. suspend fun combine() = supervisorScope { 7. val value1 = async { slowRandom(100) } 8. val value2 = async { slowRandom(200) } 9. return@supervisorScope value1.await() + value2.await() 10. }

Slide 73

Slide 73 text

Confidential Kotlin Coroutines in-depth analysis // Child coroutines - Ending 1. val job = GlobalScope.launch { 2. 3. GlobalScope.launch { 4. delay(1000) 5. System.out.println("child end") 6. } 7. } 8. 9. job.invokeOnCompletion { 10. System.out.println("parent end") 11. } Output -> parent end, child end

Slide 74

Slide 74 text

Confidential Kotlin Coroutines in-depth analysis // Child coroutines - Ending 1. val job = GlobalScope.launch { 2. 3. GlobalScope.launch(coroutineContext) { 4. delay(1000) 5. System.out.println("child end") 6. } 7. } 8. 9. job.invokeOnCompletion { 10. System.out.println("parent end") 11. } Output -> child end, parent end

Slide 75

Slide 75 text

Confidential Kotlin Coroutines in-depth analysis // Child coroutines - Cancellation 1. val job = GlobalScope.launch { 2. 3. GlobalScope.launch { 4. delay(1000) 5. System.out.println("child end") 6. } 7. } 8. 9. job.invokeOnCompletion { 10. System.out.println("parent end") 11. } 12. 13. job.cancel() Output -> parent end, child end

Slide 76

Slide 76 text

Confidential Kotlin Coroutines in-depth analysis // Child coroutines - Cancellation 1. val job = GlobalScope.launch { 2. 3. GlobalScope.launch(coroutineContext) { 4. delay(1000) 5. System.out.println("child end") 6. } 7. } 8. 9. job.invokeOnCompletion { 10. System.out.println("parent end") 11. } 12. 13. job.cancel() Output -> parent end

Slide 77

Slide 77 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine scope Coroutine Scope Coroutine Coroutine 1 Coroutine Context Job Dispatcher ... Coroutine N

Slide 78

Slide 78 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine scope 1. GlobalScope.launch(Dispatchers.IO) { 2. // something interesting 3. }

Slide 79

Slide 79 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine scope 1. object GlobalScope : CoroutineScope { 2. override val coroutineContext: CoroutineContext 3. get() = EmptyCoroutineContext 4. } 1. public interface CoroutineScope { 2. /** 3. * Context of this scope. 4. */ 5. public val coroutineContext: CoroutineContext 6. }

Slide 80

Slide 80 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine scope 1. public fun CoroutineScope.launch( 2. context: CoroutineContext = EmptyCoroutineContext, 3. start: CoroutineStart = CoroutineStart.DEFAULT, 4. block: suspend CoroutineScope.() -> Unit 5. ): Job 1. public fun CoroutineScope.async( 2. context: CoroutineContext = EmptyCoroutineContext, 3. start: CoroutineStart = CoroutineStart.DEFAULT, 4. block: suspend CoroutineScope.() -> T 5. ): Deferred

Slide 81

Slide 81 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine scope 1. class MainActivity : Activity(), CoroutineScope { 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. }

Slide 82

Slide 82 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine scope 1. class MainActivity : Activity(), CoroutineScope { 2. 3. 4. override val coroutineContext = Dispatchers.Main 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. }

Slide 83

Slide 83 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine scope 1. class MainActivity : Activity(), CoroutineScope { 2. private val job = Job() 3. 4. override val coroutineContext = Dispatchers.Main + job 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. }

Slide 84

Slide 84 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine scope 1. class MainActivity : Activity(), CoroutineScope { 2. private val job = Job() 3. 4. override val coroutineContext = Dispatchers.Main + job 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. override fun onDestroy() { 15. super.onDestroy() 16. 17. job.cancel() 18. } 19. }

Slide 85

Slide 85 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine scope 1. class MainActivity : Activity(), CoroutineScope { 2. private val job = Job() 3. 4. override val coroutineContext = Dispatchers.Main + job 5. 6. override fun onCreate() { 7. super.onCreate() 8. 9. launch { 10. // do something 11. } 12. } 13. 14. override fun onDestroy() { 15. super.onDestroy() 16. 17. job.cancel() 18. } 19. }

Slide 86

Slide 86 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine scope - Unsupervised 1. coroutineScope { 2. val job1 = async { throw Exception() } 3. val job2 = async { delay(1000) } 4. try { 5. job1.await() 6. System.out.println("job1 finished") 7. } catch (e: Exception) { 8. System.out.println("job1 crashed -> $e") 9. } 10. try { 11. job2.await() 12. System.out.println("job2 finished") 13. } catch (e: Exception) { 14. System.out.println("job2 crashed -> $e") 15. } 16. } job1 crashed: java.lang.Exception job2 crashed: JobCancellationException: Parent job is Cancelling

Slide 87

Slide 87 text

Confidential Kotlin Coroutines in-depth analysis // Coroutine scope - Supervised 1. supervisorScope { 2. val job1 = async { throw Exception() } 3. val job2 = async { delay(1000) } 4. try { 5. job1.await() 6. System.out.println("job1 finished") 7. } catch (e: Exception) { 8. System.out.println("job1 crashed -> $e") 9. } 10. try { 11. job2.await() 12. System.out.println("job2 finished") 13. } catch (e: Exception) { 14. System.out.println("job2 crashed -> $e") 15. } 16. } job1 crashed -> java.lang.Exception job2 finished

Slide 88

Slide 88 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines

Slide 89

Slide 89 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines 1. object ExternalApi { 2. fun getUser(callback: (User) -> Unit) 3. }

Slide 90

Slide 90 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines 1. object ExternalApi { 2. fun getUser(callback: (User) -> Unit) 3. } 4. 5. suspend fun getUser(): User { 6. 7. 8. 9. 10. 11. 12. }

Slide 91

Slide 91 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines 1. object ExternalApi { 2. fun getUser(callback: (User) -> Unit) 3. } 4. 5. suspend fun getUser(): User { 6. return suspendCoroutine { continuation -> 7. // non-suspend block here 8. 9. 10. 11. } 12. }

Slide 92

Slide 92 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines 1. object ExternalApi { 2. fun getUser(callback: (User) -> Unit) 3. } 4. 5. suspend fun getUser(): User { 6. return suspendCoroutine { continuation -> 7. // non-suspend block here 8. ExternalApi.getUser { user -> 9. 10. } 11. } 12. }

Slide 93

Slide 93 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines 1. object ExternalApi { 2. fun getUser(callback: (User) -> Unit) 3. } 4. 5. suspend fun getUser(): User { 6. return suspendCoroutine { continuation -> 7. // non-suspend block here 8. ExternalApi.getUser { user -> 9. continuation.resume(user) 10. } 11. } 12. }

Slide 94

Slide 94 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines 1. object ExternalApi { 2. fun getUser(callback: (User) -> Unit) 3. } 4. 5. suspend fun getUser(): User { 6. return suspendCoroutine { continuation -> 7. // non-suspend block here 8. ExternalApi.getUser { user -> 9. continuation.resume(user) 10. } 11. } 12. } 13. 14. suspend fun showUser() { 15. val user = getUser() 16. // ... 17. }

Slide 95

Slide 95 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines - Cancel 1. object ExternalApi { 2. fun getUser(callback: (User) -> Unit): Request 3. } 4.

Slide 96

Slide 96 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines - Cancel 1. object ExternalApi { 2. fun getUser(callback: (User) -> Unit): Request 3. } 4. 5. suspend fun getUser(): User { 6. 7. 8. 9. 10. 11. 12. 13. 14. }

Slide 97

Slide 97 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines - Cancel 1. object ExternalApi { 2. fun getUser(callback: (User) -> Unit): Request 3. } 4. 5. suspend fun getUser(): User { 6. return suspendCancellableCoroutine { continuation -> 7. 8. 9. 10. 11. 12. 13. } 14. }

Slide 98

Slide 98 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines - Cancel 1. object ExternalApi { 2. fun getUser(callback: (User) -> Unit): Request 3. } 4. 5. suspend fun getUser(): User { 6. return suspendCancellableCoroutine { continuation -> 7. val request = ExternalApi.getUser { user -> 8. continuation.resume(user) 9. } 10. 11. 12. 13. } 14. }

Slide 99

Slide 99 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines - Cancel 1. object ExternalApi { 2. fun getUser(callback: (User) -> Unit): Request 3. } 4. 5. suspend fun getUser(): User { 6. return suspendCancellableCoroutine { continuation -> 7. val request = ExternalApi.getUser { user -> 8. continuation.resume(user) 9. } 10. continuation.invokeOnCancellation { 11. request.cancel() 12. } 13. } 14. }

Slide 100

Slide 100 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines - Errors 1. object ExternalApi { 2. fun getUser(callback: (User?) -> Unit) 3. } 4.

Slide 101

Slide 101 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines - Errors 1. object ExternalApi { 2. fun getUser(callback: (User?) -> Unit) 3. } 4. 5. suspend fun getUser(): User { 6. return suspendCoroutine { continuation -> 7. ExternalApi.getUser { user -> 8. if (user != null) { 9. continuation.resume(user) 10. } 11. 12. 13. } 14. } 15. }

Slide 102

Slide 102 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines - Errors 1. object ExternalApi { 2. fun getUser(callback: (User?) -> Unit) 3. } 4. 5. suspend fun getUser(): User { 6. return suspendCoroutine { continuation -> 7. ExternalApi.getUser { user -> 8. if (user != null) { 9. continuation.resume(user) 10. } else { 11. continuation.resumeWithException(Exception("no user")) 12. } 13. } 14. } 15. }

Slide 103

Slide 103 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines - Errors 1. suspend fun showUser() { 2. try { 3. val user = getUser() 4. // ... 5. } catch (e: Exception) { 6. e.printStackTrace() 7. } 8. }

Slide 104

Slide 104 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines [Example] 1. fun getUser(): Single

Slide 105

Slide 105 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines [Example] 1. fun getUser(): Single 2. 3. suspend fun Single.await(): User { 4. 5. 6. 7. 8. 9. 10. 11. 12. }

Slide 106

Slide 106 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines [Example] 1. fun getUser(): Single 2. 3. suspend fun Single.await(): User { 4. return suspendCancellableCoroutine { continuation -> 5. 6. 7. 8. 9. 10. 11. } 12. }

Slide 107

Slide 107 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines [Example] 1. fun getUser(): Single 2. 3. suspend fun Single.await(): User { 4. return suspendCancellableCoroutine { continuation -> 5. getUser().subscribe({ user -> 6. continuation.resume(user) 7. }) 8. 9. 10. 11. } 12. }

Slide 108

Slide 108 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines [Example] 1. fun getUser(): Single 2. 3. suspend fun Single.await(): User { 4. return suspendCancellableCoroutine { continuation -> 5. getUser().subscribe({ user -> 6. continuation.resume(user) 7. }, { error -> 8. continuation.resumeWithException(error) 9. }) 10. 11. } 12. }

Slide 109

Slide 109 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines [Example] 1. fun getUser(): Single 2. 3. suspend fun Single.await(): User { 4. return suspendCancellableCoroutine { continuation -> 5. val disposable = getUser().subscribe({ user -> 6. continuation.resume(user) 7. }, { error -> 8. continuation.resumeWithException(error) 9. }) 10. continuation.invokeOnCancellation { disposable.dispose() } 11. } 12. }

Slide 110

Slide 110 text

Confidential Kotlin Coroutines in-depth analysis // Suspended coroutines [Example] 1. fun getUser(): Single 2. 3. suspend fun showUser() { 4. try { 5. val user = getUser().await() 6. // ... 7. } catch (e: Exception) { 8. e.printStackTrace() 9. } 10. }

Slide 111

Slide 111 text

Confidential Kotlin Coroutines in-depth analysis // Testing Coroutines

Slide 112

Slide 112 text

Confidential Kotlin Coroutines in-depth analysis // Testing Coroutines 1. suspend fun mySuspendFunction(): String { 2. return "My Test String" 3. } 4. 5. @Test 6. fun mySuspendFunctionTest() { 7. 8. }

Slide 113

Slide 113 text

Confidential Kotlin Coroutines in-depth analysis // Testing Coroutines 1. suspend fun mySuspendFunction(): String { 2. return "My Test String" 3. } 4. 5. @Test 6. fun mySuspendFunctionTest() { 7. val job = GlobalScope.async { 8. mySuspendFunction() 9. } 10. job.invokeOnCompletion { 11. assertEquals(job.getCompleted(), "My Test String") 12. } 13. }

Slide 114

Slide 114 text

Confidential Kotlin Coroutines in-depth analysis // Testing Coroutines 1. suspend fun mySuspendFunction(): String { 2. return "My Test String" 3. } 4. 5. @Test 6. fun mySuspendFunctionTest() { 7. 8. }

Slide 115

Slide 115 text

Confidential Kotlin Coroutines in-depth analysis // Testing Coroutines 1. suspend fun mySuspendFunction(): String { 2. return "My Test String" 3. } 4. 5. @Test 6. fun mySuspendFunctionTest() { 7. val result = runBlocking { 8. mySuspendFunction() 9. } 10. assertEquals(result, "My Test String") 11. }

Slide 116

Slide 116 text

Confidential Kotlin Coroutines in-depth analysis // Testing Coroutines 1. suspend fun mySuspendFunction(): String { 2. return "My Test String" 3. } 4. 5. @Test 6. fun mySuspendFunctionTest() = runBlocking { 7. assertEquals(mySuspendFunction(), "My Test String") 8. }

Slide 117

Slide 117 text

Confidential Kotlin Coroutines in-depth analysis // Benchmarks

Slide 118

Slide 118 text

Confidential Kotlin Coroutines in-depth analysis // Benchmarks 1. for (i in 0..1_000_000) { 2. launch(dispatcher) { 3. System.out.println("$i") 4. } 5. } 1. for (i in 0..1_000_000) { 2. executor.execute { 3. System.out.println("$i") 4. } 5. } finished in 4.219 sec finished in 3.853 sec ~10% overhead

Slide 119

Slide 119 text

Confidential Kotlin Coroutines in-depth analysis // Final thoughts

Slide 120

Slide 120 text

Confidential Kotlin Coroutines in-depth analysis // Final thoughts 1. Sequences and channels were not covered 2. Reflections don’t work with coroutines 3. No way to call coroutines from Java (without kotlin wrapper) 4. Coroutines debugger bugs: a. Variables inside coroutine are not visible b. Evaluate expression doesn’t work c. Breakpoint after last suspension point doesn’t trigger

Slide 121

Slide 121 text

// Obligatory Q&A

Slide 122

Slide 122 text

// Thank you.