Using coroutines for asynchronous, concurrent execution of tasks
In this recipe, we are going to explore how to use the coroutines framework in order to schedule asynchronous, concurrent execution of tasks. We are going to learn both how to synchronize a sequence of short background tasks and how to run expensive, long-running ones at the same time. We will simulate the sushi rolls preparation process to discover how to schedule blocking and non-blocking tasks together.
Getting ready
The first step to start working with Kotlin Coroutines is to add the core framework dependency to the project:
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.23.3'
The preceding code declares the kotlinx-coroutines-core
dependency in a Gradle build script, which is used in the sample project (https://github.com/PacktPublishing/Kotlin-Standard-Library-Cookbook).
In the current recipe, we will assume our sushi-cooking simulation requires the four following steps to be performed:
- Cook the rice
- Prepare...