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

Kotlin Coroutine to the rescue!

Kotlin Coroutine to the rescue!

Asynchronous or non-blocking programming is the new reality. Whether we're creating server-side, desktop or mobile applications, using microservices architecture or not, it provides an experience that is not only fluid from the user's perspective, but scalable when needed. Coroutines is a new feature that was released in Kotlin 1.3 and has similar counterparts in other languages like C# and JavaScript. Let’s understand together what it is all about.


adi polak

May 15, 2019

More Decks by adi polak

Other Decks in Programming


  1. Coroutine to the rescue! Adi Polak

  2. About me – Adi Polak @adipolak https://medium.com/@adipolak https://dev.to/adipolak

  3. None
  4. Kotlin: 1. Developed by JetBrains (IntelliJ authors) 2. Powers all

    JetBrains tools 3. Open source 4. 100% interoperable with Java 5. First language on Android 6. Provides: a. Null safety b. Higher order function c. Data classes d. Extension functions e. Since Kotlin 1.1 – coroutines f. … MORE! @AdiPolak L E T ’ S S TA R T F R O M T H E B E G I N N I N G :
  5. Kotlin coroutines

  6. C o r o u t i n e s

    c o m m o n u s e c a s e s • Asynchronous • Non blocking @AdiPolak • Server side - microservices • Android app development • … W h y ?
  7. C a n w e c o m p a

    r e c o r o u t i n e a n d t h r e a d s ? @AdiPolak
  8. CPU CORE 1 Thread 1 Thread 2 Thread 3 CONCURENCY

    IN THREADS CPU CORE 2 Thread 1 Thread 2 Thread 3 @AdiPolak
  9. Kotlin runtime Coroutine 1 Coroutine 2 Coroutine 3 CONCURENCY IN

    COROUTINES Coroutine 1 Coroutine 3 @AdiPolak

  11. suspend fun someBackgroundTask(param: List<String>): Int { // long running operation

    return 7 } fun someBackgrounTask(param: List<String>, callback: Continuation<Int>): Int { // long running operation return 7 } SUSPENDING FUNCTIONS @AdiPolak
  12. Job Dispatcher COROUTINES CONTEXT AND SCOPE @AdiPolak Dispatchers.Default Dispatchers.Main Dispatchers.IO

    Dispatchers.Unconfined fun CoroutineScope.launch( context: CoroutineContext = EmptyCoroutineContext, ..): Job coroutineContext[Job]
  13. BUILDERS @AdiPolak Extending scope functions: Launch Async Can be used

    from Main thread: runBlocking
  14. COROUTINES CONTEXT, SCOPE AND BUILDERS @AdiPolak coroutineScope { launch(Dispatchers.Default +

    CoroutineName(”some name")) { doSuspendedTask() } }
  15. CHANNELS @AdiPolak val channel1 = Channel<Any>(capacity = Channel.RENDEZVOUS) val channel2

    = Channel< Any >(capacity = Channel.CONFLATED) val channel3 = Channel< Any >(capacity = 10) val channel4 = Channel< Any >(capacity = Channel.UNLIMITED) https://proandroiddev.com/kotlin-coroutines-channels-csp-android-db441400965f Channel is a non-blocking primitive for communication between sender using [SendChannel] and receiver using [ReceiveChannel].
  16. Demo https://github.com/adipola/kotlin-coroutine-transaction-process-mock

  17. • Ktor • Vert.x • Arrow • … @AdiPolak Frameworks

  18. Summary • What and why coroutines are important • Coroutine

    and threads • Key concepts • Demo • Frameworks @AdiPolak
  19. Questions