Coroutines in Kotlin

Kotlin offers a better way to perform asynchronous tasks, called coroutines. Coroutines are a new and fluent way of writing asynchronous and non-blocking code in Kotlin. They are much lighter than threads and are easy to manage. Both coroutines and threads are multitasking, but they differ in that threads are managed by the kernel and coroutines are managed by the code itself, thereby giving programmatic control.

Coroutines were introduced in Kotlin 1.1 as an experimental feature. There are two types of coroutines:

  • Stackful
  • Stackless

A stackful coroutine is a normal function that can be suspended during execution. It will be suspended with its entire stack, including its local variables, and the parameters passed from ...

Get Kotlin for Enterprise Applications using Java EE now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.