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

Coroutines and Asynchronous Programming (BlrKotlin)

Coroutines and Asynchronous Programming (BlrKotlin)

Presented at BlrKotlin March Meetup (2019), this talks guids you through the basic concepts of Coroutines and how to use them for you asynchronous programming requirement in Android

Rivu Chakraborty

March 23, 2019
Tweet

More Decks by Rivu Chakraborty

Other Decks in Technology

Transcript

  1. Rivu Chakraborty BYJU’S @rivuchakraborty About Me • Sr Software Engineer

    (Android) - BYJU’S • Instructor - Caster.io • Google Certified Associate Android Developer • DroidJam Speaker • Author - Reactive Programming in Kotlin • Author - Functional Kotlin • Author - Hands on Data Structures and Algorithms with Kotlin • Author - Coroutines for Android Developers (WIP) 3 https://www.rivu.dev
  2. Concurrency • Ability to execute multiple code blocks at the

    same time @rivuchakraborty https://www.rivu.dev
  3. Concurrency • Ability to execute multiple code blocks at the

    same time • Not only for Android Developers @rivuchakraborty https://www.rivu.dev
  4. Concurrency • Ability to execute multiple code blocks at the

    same time • Not only for Android Developers • JavaScript - Promise • JVM, Android - Threads (Fibers) • Native (LLVM) - Pure Kotlin @rivuchakraborty https://www.rivu.dev
  5. 11 Concurrency in Android • AsyncTask • CompletableFuture • Runnable

    / Thread (Custom Threadpool) • Rx • Counting... @rivuchakraborty https://www.rivu.dev
  6. 12 Concurrency in Android • Coroutines @rivuchakraborty https://www.rivu.dev Let’s you

    write non-blocking asynchronous code in your choice of Style - Sequentially, in Functional Style or whatever you prefer.
  7. Coroutines 13 @rivuchakraborty https://www.rivu.dev Light-Weight Threads Thread 1 Thread 2

    Thread 3 Coroutine 1 Coroutine 2 Coroutine 3 With Coroutines, Threads are still used (for JVM).
  8. Coroutines 14 @rivuchakraborty https://www.rivu.dev Light-Weight Threads Thread 1 Thread 2

    Thread 3 Coroutine 1 Coroutine 2 C 3 C 4 C 5 C 6 C 7 C 8 C N But a single Thread (Fiber) can run multiple coroutines.
  9. Get the Job done or cancel it, your choice. :)

    21 @rivuchakraborty https://www.rivu.dev
  10. Suspending Function 25 @rivuchakraborty https://www.rivu.dev • suspend is a Keyword

    in Kotlin • Compiler level restriction - can’t call suspend function outside CoroutineScope
  11. Suspending Function 26 @rivuchakraborty https://www.rivu.dev • suspend is a Keyword

    in Kotlin • Compiler level restriction - can’t call suspend function outside CoroutineScope • Suspends execution of current coroutine, until the function is completed
  12. Coroutine Scope 28 @rivuchakraborty https://www.rivu.dev • Container of CoroutineContext •

    Every Coroutine Builder is an extension over CoroutineScope, and thus inherits its CoroutineContext
  13. Coroutine Scope 29 @rivuchakraborty https://www.rivu.dev • Container of CoroutineContext •

    Every Coroutine Builder is an extension over CoroutineScope, and thus inherits its CoroutineContext • All Coroutines must be launched within a CoroutineScope
  14. Coroutine Context 31 @rivuchakraborty https://www.rivu.dev • Holds key informations about

    the Coroutine such as Dispatcher / ThreadPool Configuration, Coroutine Name etc.