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

Efficient Async Coding in Kotlin Coroutines - B...

Efficient Async Coding in Kotlin Coroutines - BoJUG

Presented at BoJUG, January 2019 conference, this presentation walks you through the basics of Coroutines and guides you in implementing them in your project.

Rivu Chakraborty

January 19, 2019
Tweet

More Decks by Rivu Chakraborty

Other Decks in Technology

Transcript

  1. Rivu Chakraborty Efficient Async Coding in Kotlin Coroutines 1 Bangalore

    JAVA User Group (BoJUG) - Year’s First Meetup - Full Day - Informatica - Jan, 19, 2019 19/01/19
  2. 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 - Coroutines for Android Developers (WIP)
  3. Concurrency • Ability to execute multiple code blocks at the

    same time • Not only for Android Developers @rivuchakraborty
  4. Concurrency • Ability to execute multiple code blocks at the

    same time • Not only for Android Developers • JavaScript - Promise • JVM, Android - Threads @rivuchakraborty
  5. 10 Concurrency in Android and/or JVM • AsyncTask (Android only)

    • CompletableFuture (Android - API 24+, JVM 1.8) • Runnable / Thread (Custom Threadpool) • Rx • Counting... @rivuchakraborty
  6. 13 Concurrency in Android • Rx @rivuchakraborty If you’re using

    Rx only for concurrency, sorry pal, you’re doing it wrong.
  7. 14 • Coroutines @rivuchakraborty Let’s you write non-blocking asynchronous code

    in your choice of Style - Sequentially, in Functional Style or whatever you prefer.
  8. 15 • Coroutines @rivuchakraborty Let’s you write non-blocking asynchronous code

    in your choice of Style - Sequentially, in Functional Style or whatever you prefer. A Language level feature, managed by the Kotlin team itself.
  9. 16 • Coroutines @rivuchakraborty • Let’s you write non-blocking asynchronous

    code in your choice of Style - Sequentially, in Functional Style or whatever you prefer. • A Language level feature, managed by the Kotlin team itself. You can add Coroutines dependency to your Kotlin project by following the instructions in GitHub ReadMe - https://github.com/kotlin/kotlinx.coroutines/blob/master/README.md#u sing-in-your-projects
  10. Coroutines 17 @rivuchakraborty Light-Weight Threads Thread 1 Thread 2 Thread

    3 Coroutine 1 Coroutine 2 Coroutine 3 With Coroutines, Threads are still used (for JVM).
  11. Coroutines 18 @rivuchakraborty 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 can run multiple coroutines.
  12. Suspending Function 30 @rivuchakraborty • suspend is a Keyword in

    Kotlin • Compiler level restriction - can’t call suspend function outside CoroutineScope
  13. Suspending Function 31 @rivuchakraborty • suspend is a Keyword in

    Kotlin • Compiler level restriction - can’t call suspend function outside CoroutineScope • Suspends execution of current coroutine
  14. Coroutine Scope 33 @rivuchakraborty • Container of CoroutineContext • Every

    Coroutine Builder is an extension over CoroutineScope, and thus inherits its CoroutineContext
  15. Coroutine Scope 34 @rivuchakraborty • Container of CoroutineContext • Every

    Coroutine Builder is an extension over CoroutineScope, and thus inherits its CoroutineContext • All Coroutines must be launched within a CoroutineScope
  16. Dispatchers 36 @rivuchakraborty • Determines which Thread / ThreadPool, the

    coroutine will run on. • Similar to the Rx Schedulers
  17. Rivu Chakraborty @rivuchakraborty Resources https://caster.io/courses/kotlin-coroutines-fundamental s • http://bit.ly/CodeLabCoroutines (Codelab) •

    http://bit.ly/CoroutinesAndroid (Article) • http://bit.ly/DroidCoroutines (Book, yet to be published) • http://bit.ly/CoroutinesGuide (Official Guide) • https://www.meetup.com/BlrKotlin/ (Bangalore Kotlin User Group) 43