Kotlin coroutines - A comprehensive introduction

Click for: original source

This article introduces Kotlin coroutines, a powerful tool for asynchronous programming. Kotlin’s coroutines fall under the umbrella of structured concurrency. They implement a model of concurrency which you can consider similar to Java virtual threads, Cats Effect and ZIO fibers. By Daniel CiocĂ®rlan.

The article requires a minimum knowledge of the Kotlin language. Still, you should be fine if you come from a Scala background. The topics discussed:

  • Background and setup
  • Why coroutines?
  • Suspending functions
  • Coroutine scope and structural concurrency
  • Coroutine builders
  • Cooperative scheduling
  • Cancellation

Our journey through the basics of the Kotlin coroutines library is over. We saw why coroutines matter and made a simplified explanation of how they’re implemented under the hood. Then, we showed how to create coroutines, also introducing the structural concurrency topic. We saw how cooperative scheduling and cancellation work with many examples. Finally, we introduced the main features of the coroutines’ context. There is a lot more to say about coroutines, but we hope this article can be a good starting point for those who want to learn more about them. Good read!

[Read More]

Tags kotlin java app-development programming jvm distributed