In Kotlin coroutines are first-class citizens. They’re built into the language, but the convenience functions to work with coroutines are part of a library. Coroutines offer some capabilities that aren’t possible with subroutines. They’re used in infinite sequences, event loops, and cooperating functions, for example. To learn about the benefits of coroutines, we’ll start with an example that runs sequentially and then change the code to run concurrently with coroutines.

When executing code, you can decide if the code should run sequentially or if it should run concurrently, either within the context of the current execution or as a separate coroutine. Let’s explore how we can configure these options.

Starting with sequential execution

Let’s start with a piece of code that executes function calls sequentially—this will serve as a building block for introducing coroutines.

Get hands-on with 1200+ tech skills courses.