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

Kotlin Coroutines

Kotlin Coroutines

Slide for the talk on Coroutines at #ADAyWithKotlin by #KotlinKolkata on 25/11/2017.
Github link for the project: https://github.com/RivuChk/CoroutineTalk

Rivu Chakraborty

November 25, 2017
Tweet

More Decks by Rivu Chakraborty

Other Decks in Technology

Transcript

  1. Rivu Chakraborty Rivu Chakraborty is a Google Certified Android Developer,

    Sr. Tech Member of Institute of Engineers (India), an Author of multiple Kotlin books, he also have certifications on Scrum. Having total 5+ years of experience he is presently working as a Sr. Software Engineer (Android) at Indus Net Technologies Pvt. Ltd. Rivu Chakraborty considers himself a Kotlin and Android enthusiast and a Kotlin evangelist. He has been using Kotlin since December 2015, so he has around 2 years' experience in Kotlin. As part of his mission to expand the use and knowledge of the Kotlin Language as much as possible, he created the KotlinKolkata User Group, one of the most active Kotlin user groups throughout the world and he is a Founder Organizer of KotlinKolkata UG. He is also an active member of GDG Kolkata and gives talks at GDG Kolkata Meetups. As Rivu believes that knowledge increases by sharing, he writes lot of tutorials at JavaCodeGeeks, AndroidHive and at his own site (http://www.rivuchk.com). Please visit his website http://www.rivuchk.com to learn more about him and to read his tutorials.
  2. Books by Rivu Chakraborty Reactive Programming in Kotlin (Packt) https://www.packtpub.com/ap

    plication-development/reactive- programming-kotlin Functional Kotlin (Packt) Co-Author – Mario Arias Coroutines for Android Developers Co-Author – Ravindra Kumar https://leanpub.com/coroutines- for-android-developers
  3. Kotlin Kolkata User Group Mission: Spread the knowledge and use

    of Kotlin Language throughout the region. 1 Help professionals out there using Kotlin 2 Help other Kotlin User Groups 3
  4. What are Coroutines?  Path breaking and, probably, the most

    exciting feature in Kotlin are coroutines.  A new way to write asynchronous, non-blocking code somewhere like the threads, but way  Simpler, more efficient, and lightweight.  Coroutines were added in Kotlin 1.1 and are still experimental  Computation that can be suspended without blocking a thread  No context switching
  5. Suspend  Suspending functions are Kotlin functions with a added

    keyword suspend. This keyword denotes that calls to these functions may suspend a coroutine.
  6. Launch and Async  Launch and Async both work similarly,

    they execute provided block of code in the provided Coroutine context (CommonPool if not mentioned explicitly)  Except that async can return a value, but launch cannot. Which one to use? Await  With await function, we can suspend a coroutine and wait for a variable to get it’s value.  Coroutine will not be suspended if the variable already got its value.
  7. Job and Join  A Job in the Coroutine Context

    is the coroutine itself.  As with variable (with await()), we can also wait for a job to complete.  The join operator helps us wait for a job, but it must be called within another coroutine context  Basically the join function joins the coroutine with the parent coroutine It was called inside, keeping their Contexts as it is. **PS runBlocking block is also a coroutine
  8. Available Coroutine Contexts  Unconfined: It suspends the currently running

    context and runs right there.  CommonPool: Represents common pool of shared threads as coroutine dispatcher for compute-intensive tasks.  newSingleThreadContext: Creates new coroutine execution context with the a single thread and built-in [yield] and [delay] support.  To get currently running Coroutine context just use coroutineContext inside a coroutine.
  9. Generators API in kotlin.coroutines  The only "application-level" functions in

    kotlin.coroutines.experimental  These are shipped within kotlin-stdlib because they are related to sequences. In fact, these functions (and we can limit ourselves to buildSequence() alone here) implement generators, i.e. provide a way to cheaply build a lazy sequence.  Read more at: https://kotlinlang.org/docs/reference/coroutines.html#generators-api-in- kotlincoroutines
  10. Further Read  Kotlin for Android Developers - https://leanpub.com/kotlin-for-android- developers

     Coroutines for Android Developers - https://leanpub.com/coroutines-for- android-developers  Coroutines Reference - https://kotlinlang.org/docs/reference/coroutines.htm