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

Coroutines II : Advanced Kotlin Coroutine Concepts

Coroutines II : Advanced Kotlin Coroutine Concepts

This talks takes a deeper dive into coroutines concepts like Jobs and their lifecycle

Brian Odhiambo

June 15, 2023
Tweet

More Decks by Brian Odhiambo

Other Decks in Programming

Transcript

  1. 02 Design Centric Engineer Brian Odhiambo Dishwasher @ Baobab Circle

    Co-organizer @ KotlinKenya Maintaining @ KotlinBits Yeah am all about that UI/UX & Kotlin
  2. Concurrency but... Structured 03 jobs! jobs! jobs! Jobs What are

    Jobs and what is their lifecycle... Builders How can you use builders to create coroutines Contexts What are contexts and what is context switching
  3. suited when no result is needed Scope.launch { ... }

    suited when a result is needed Scope.async { ... } What are coroutine builders? serve as extensions to CoroutineScope gives you more control over lifecycle and behaviour of a coroutine return a Job Functions which are used to launch coroutines
  4. An Example Builders launch & async import kotlinx.coroutines.* suspend fun

    main() { val job = GlobalScope.launch { println("Searching for a Book") } val job2 = GlobalScope.async { println("Currently in the Library") delay(1000) "Found : Kotlin In Action Edition 2" } println(job) println(job2) println("-".repeat(20)) println(job2.await()) Thread.sleep(2000) } "coroutine#1":StandaloneCoroutine{Active}@5479e3f "coroutine#2":DeferredCoroutine{Active}@42f30e0a -------------------- Currently in the Library Searching for a Book Found : Kotlin In Action Edition 2 06
  5. dictates how the coroutines related to threads dispatcher represent the

    cancellable task needed to run job What's a coroutine context? represented CoroutineContext interface contain a Job and Dispatcher used to execute the coroutine Immutable key-value pair that contains various data available to the coroutine
  6. An Example Coroutine context in purple import kotlinx.coroutines.* fun CoroutineScope.working(){

    println("I'm working in thread ${Thread.currentThread().name}") } fun main() = runBlocking<Unit> { launch { working() } launch(Dispatchers.Unconfined) { working() } launch(Dispatchers.Default) { working() } launch(CoroutineName("MyOwnThread")) { working() } } I'm working in thread main @coroutine#3 I'm working in thread DefaultDispatcher-worker-1 @coroutine#4 I'm working in thread main @coroutine#2 I'm working in thread main @MyOwnThread#5 06
  7. What is context switching? running a coroutine in a different

    thread Changing the context in which a coroutine runs Example import kotlinx.coroutines.* suspend fun main(){ doNetworkStuff() } suspend fun updateUi(){ withContext(Dispatchers.Main){ ... } } suspend fun doNetworkStuff(){ withContext(Dispatchers.IO){ ... updateUi() } }
  8. new created but not started start() / join() What are

    jobs? track task states and cancel check if isActive, isCancelled, isCompleted The lifecycle of the concurrent task active started but not completed completing waiting for children to complete completed finished and completed children
  9. An Example Lifecycle check & update import kotlinx.coroutines.* suspend fun

    main() { val job = GlobalScope.launch { var i = 1 while(isActive){ println("Value : ${i++}") } } delay(20) job.cancel() } -------------------------------------- Value : 1 Value : 2 Value : 3 Value : 4 Value : 5 ... 06
  10. Dispatchers Android specific dispatchers and where they are used In-Depth

    Guide to Android Coroutines 14 Scopes Different android specific scopes and where to use each and so much more!
  11. KotlinBits 😎 A fun and easy way to learn Kotlin

    in small bits and pieces. React, Comment & Share https://kotlinbits.vercel.app