Running Concurrently Using Coroutines

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 ...

Get Programming Kotlin 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.