An introduction to Kotlin Coroutines

An introduction to KotlinΒ Coroutines

Coroutines that fascinated devs from its early beta have graduated the experimental phase and are now stable πŸŽ“. Coroutines provide a way to write asynchronous code sequentially making multithreaded programming more debuggable and maintainable. In this presentation Antonis will try to guide you through the main features of Coroutines that make it so special.
An article based on this presentation can be found at https://antonis.me/2018/12/12/an-introduction-to-kotlin-coroutines/

851ab31c172ebb86ad592b6c7f844056?s=128

Antonis Lilis

December 05, 2018
Tweet

Transcript

  1. 2.

    The Problem How to prevent our applications from blocking β€’

    Asynchronous or non-blocking programming is the new reality β—¦ Fluid client experience β—¦ Scalable server architecture
  2. 3.

    Approaches β€’ Threads β—¦ hard to write and maintain β€’

    Callbacks β—¦ series of nested callbacks which lead to incomprehensible code β€’ Futures, Promises,... β—¦ different programming mental model β€’ Reactive Extensions β—¦ everything is a stream, and it's observable β€’ Coroutines
  3. 4.

    Coroutines β€’ Based on the concept of suspending functions β€’

    The code is still structured as if we were writing synchronous code β€’ Are like light-weight threads The term 'Coroutine' was coined by Melvin Conway in 1958 (known for Conway's Law)
  4. 5.

    Kotlin Coroutines β€’ Kotlin provides Coroutine support at the language

    level β—¦ Actually it only adds one language keyword (suspend) β€’ Functionality is delegated to libraries β—¦ kotlinx.coroutines is a library developed by JetBrains β€’ Since Kotlin 1.3 Coroutines are no longer experimental β—¦ The major feature of this release
  5. 12.

    Suspending Functions β€’ Used inside coroutines like regular functions β€’

    They can call other suspending functions β€’ Waits tasks to complete
  6. 14.

    Coroutine Builders β€’ Create a coroutine and provide a CoroutineScope

    β€’ Examples are runBlocking, launch, async etc β€’ GlobalScope.launch creates a top-level coroutine (like a Thread)
  7. 15.

    CoroutineScope β€’ Coroutines are launched in the scope of the

    operation we are performing β€’ We can declare a scope using coroutineScope builder
  8. 16.

    CoroutineContext β€’ Is an an optional parameter of all coroutine

    builders β€’ Includes a coroutine dispatcher that determines the execution thread β€’ inherited from the CoroutineScope if not defined
  9. 17.

    Coroutine Cancelation β€’ A coroutine code has to cooperate to

    be cancellable β€’ All the suspending functions in kotlinx.coroutines are cancellable
  10. 18.

    Concurrency is not Parallelism β€’ Parallelism is about the execution

    of multiple tasks at the same time β€’ Concurrency tries to break down tasks which we don’t necessarily need to execute at the same time β€’ Concurrency's primary goal is structure, not parallelism. β€’ Concurrency makes the use of parallelism easier
  11. 19.

    Structured Concurrency β€’ launch is a child of coroutineScope β€’

    the scope waits for the completion of all children β€’ in case of a crash the scope cancels all children β€’ the suspend function has no leaks
  12. 20.

    Exceptions β€’ An exception other than CancellationException in a coroutine

    cancels its parent β€’ A CoroutineExceptionHandler may be passed to the context to replace try /catch blocks β€’ If we want cancellation to be propagated only downwards we use SupervisorJob or supervisorScope
  13. 21.

    State β€’ shared mutable state βž” share by communicating β€’

    classes/objects βž” coroutines β€’ synchronization primitives βž” communication primitives
  14. 24.
  15. 25.

    Final Thoughts β€’ Coroutines are NOT like threads β€’ Force

    us to rethink the way we structure our code β€’ Intend to look like sequential code and hide the complicated stuff β€’ Resource-wise are almost free β€’ Coroutines are the cool new thing in the JVM world
  16. 26.

    References β€’ Source Examples https://github.com/antonis/CoroutinesExamples β€’ kotlinlang.org https://kotlinlang.org/docs/reference/coroutines-overview.html β€’ KotlinConf

    2018: Exploring Coroutines in Kotlin by Venkat Subramariam https://youtu.be/jT2gHPQ4Z1Q β€’ KotlinConf 2018: Kotlin Coroutines in Practice by Roman Elizarov https://youtu.be/a3agLJQ6vt8 β€’ Concurrent Coroutines - Concurrency is not parallelism by Simon Wirtz https://kotlinexpertise.com/kotlin-coroutines-concurrency/