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

Kotlin Coroutines

Rygel Louv
November 14, 2020

Kotlin Coroutines

Introduction to Kotlin Coroutines. We went through the basic concepts of Coroutines.

Rygel Louv

November 14, 2020
Tweet

More Decks by Rygel Louv

Other Decks in Programming

Transcript

  1. - Simplifying asynchronous programming
 - Write async code that looks

    like sync code
 Coroutines: what problem do they solve ? P . 3
  2. What is a coroutine ? P . 3 - Not

    a thread
 - Takes a piece of code and run it in a thread
 - Lightweight thread
 

  3. - Dispatcher.Main: Main thread
 - Dispatcher.IO: Network and disk
 -

    Dispatcher.Default: CPU (heavy computation)
 
 Coroutines dispatchers P . 3
  4. - Canceling coroutine execution
 - Following a Lifecycle
 - Informing

    components when Exception happens
 
 Coroutines concepts: Structured concurrency P . 3 Avoid memory leaks
  5. - Keep track of coroutines
 - Ability to cancel them


    - Is notified of failures
 
 Structure concurrency: Scopes P . 3
  6. P . 3 Coroutine scope: launching coroutines Parent Child Scope

    will get all exceptions thrown by this function
  7. - When a child fails, the scope get notified
 -

    Then the scope cancel itself and cancel all children
 Scopes exception handling: Job P . 3
  8. - The failure of one child does not affect other

    children
 - When the scope get notified of a failure, nothing happens
 Scopes exception handling: SupervisorJob P . 3
  9. Creating coroutines: Launch P . 3 - Creates a new

    coroutine
 - Fire and forget

  10. Creating coroutines: Async P . 3 - Creates a new

    coroutine
 - Returns a value
 Deferred == Promise/Future
  11. Next on coroutines P . 3 - Dive deeper in

    Continuation Passing Style - More on co-operation - Channels - Flows