When an Android application is first started, the runtime system creates a single thread in which all components will run by default. This thread is generally referred to as the main thread. The primary role of the main thread is to handle the user interface in terms of event handling and interaction with views in the user interface. Any additional components started within the application will, by default, also run on the main thread.
Any code within an application that performs a time-consuming task using the main thread will cause the entire application to appear to lock up until the task is completed. This typically results in the operating system displaying an “Application is not responding” warning to the user. This is far from the desired behavior for any application. Fortunately, Kotlin provides a lightweight alternative in the form of Coroutines. This chapter will introduce Coroutines, including terminology such as dispatchers, coroutine scope, suspend functions, coroutine builders, and structured concurrency. The chapter will also explore channel-based communication between coroutines.
What are Coroutines?
Coroutines are blocks of code that execute asynchronously without blocking the thread from which they are launched. Coroutines can be implemented without worrying about building complex AsyncTask implementations or directly managing multiple threads. Because of the way they are implemented, coroutines are much more efficient and less resource intensive than using traditional multi-threading options. Coroutines also make for code that is much easier to write, understand and maintain since it allows code to be written sequentially without having to write callbacks to handle thread-related events and results.
Although a relatively recent addition to Kotlin, there is nothing new or innovative about coroutines. Coroutines, in one form or another, have existed in programming languages since the 1960s and are based on a model known as Communicating Sequential Processes (CSP). Though it does so efficiently, Kotlin still uses multithreading behind the scenes.
Threads vs. Coroutines
A problem with threads is that they are a finite resource and expensive in terms of CPU capabilities and system overhead. In the background, much work is involved in creating, scheduling, and destroying a thread. Although modern CPUs can run large numbers of threads, the actual number of threads that can be run in parallel at any one time is limited by the number of CPU cores (though newer CPUs have 8 cores, most Android devices contain CPUs with 4 cores). When more threads are required than there are CPU cores, the system has to perform thread scheduling to decide how the execution of these threads is to be shared between the available cores.
To avoid these overheads, instead of starting a new thread for each coroutine and destroying it when the coroutine exits, Kotlin maintains a pool of active threads and manages how coroutines are assigned to those threads. When an active coroutine is suspended, the Kotlin runtime saves it, and another coroutine resumes to take its place. When the coroutine is resumed, it is restored to an existing unoccupied thread within the pool to continue executing until it either completes or is suspended. Using this approach, a limited number of threads are used efficiently to execute asynchronous tasks with the potential to perform large numbers of concurrent tasks without the inherent performance degeneration that would occur using standard multi-threading.
Coroutine Scope
All coroutines must run within a specific scope, allowing them to be managed as groups instead of as individual ones. This is particularly important when canceling and cleaning up coroutines, for example, when a Fragment or Activity is destroyed, and ensuring that coroutines do not “leak” (in other words, continue running in the background when the app no longer needs them). By assigning coroutines to a scope, they can, for example, all be canceled in bulk when they are no longer needed.
Kotlin and Android provide built-in scopes and the option to create custom scopes using the CoroutineScope class. The built-in scopes can be summarized as follows:
- GlobalScope – GlobalScope is used to launch top-level coroutines tied to the entire application lifecycle. Since this has the potential for coroutines in this scope to continue running when not needed (for example, when an Activity exits), use of this scope is not recommended for Android applications. Coroutines running in GlobalScope are considered to be using unstructured concurrency.
- ViewModelScope – Provided specifically for ViewModel instances when using the Jetpack architecture ViewModel component. Coroutines launched in this scope from within a ViewModel instance are automatically canceled by the Kotlin runtime system when the corresponding ViewModel instance is destroyed.
- LifecycleScope – Every lifecycle owner has associated with it a LifecycleScope. This scope is canceled when the corresponding lifecycle owner is destroyed, making it particularly useful for launching coroutines from within activities and fragments.
For all other requirements, a custom scope will likely be used. The following code, for example, creates a custom scope named myCoroutineScope:
private val myCoroutineScope = CoroutineScope(Dispatchers.Main)
Code language: Kotlin (kotlin)
The coroutineScope declares the dispatcher that will be used to run coroutines (though this can be overridden) and must be referenced each time a coroutine is started if it is to be included within the scope. All of the running coroutines in a scope can be canceled via a call to the cancel() method of the scope instance:
myCoroutineScope.cancel()
Code language: Kotlin (kotlin)
Suspend Functions
A suspend function is a special type of Kotlin function that contains the code of a coroutine. It is declared using the Kotlin suspend keyword, which indicates to Kotlin that the function can be paused and resumed later, allowing long-running computations to execute without blocking the main thread. The following is an example suspend function:
suspend fun mySlowTask() {
// Perform long-running tasks here
}
Code language: Kotlin (kotlin)
Coroutine Dispatchers
Kotlin maintains threads for different types of asynchronous activity, and when launching a coroutine, it will be necessary to select the appropriate dispatcher from the following options:
- Dispatchers.Main – Runs the coroutine on the main thread and is suitable for coroutines that need to make changes to the UI and as a general-purpose option for performing lightweight tasks.
- Dispatchers.IO – Recommended for coroutines that perform network, disk, or database operations.
- Dispatchers.Default – Intended for CPU-intensive tasks such as sorting data or performing complex calculations.
The dispatcher is responsible for assigning coroutines to appropriate threads and suspending and resuming the coroutine during its lifecycle. In addition to the predefined dispatchers, it is also possible to create dispatchers for your own custom thread pools.
Coroutine Builders
The coroutine builders bring together all of the components covered so far and launch the coroutines so that they start executing. For this purpose, Kotlin provides the following six builders:
- launch – Starts a coroutine without blocking the current thread and does not return a result to the caller. Use this builder when calling a suspend function from within a traditional function and when the results of the coroutine do not need to be handled (sometimes referred to as “fire and forget” coroutines).
- async – Starts a coroutine and allows the caller to wait for a result using the await() function without blocking the current thread. Use async when you have multiple coroutines that need to run in parallel. The async builder can only be used from within another suspend function.
- withContext – Allows a coroutine to be launched in a different context from that used by the parent coroutine. Using this builder, a coroutine running using the Main context could launch a child coroutine in the Default context. The withContext builder also provides a useful alternative to async when returning results from a coroutine.
- coroutineScope – The coroutineScope builder is ideal for situations where a suspend function launches multiple coroutines that will run in parallel and where some action must occur only when all the coroutines reach completion. If those coroutines are launched using the coroutineScope builder, the calling function will not return until all child coroutines have completed. When using coroutineScope, a failure in any coroutine will cancel all other coroutines.
- supervisorScope – Similar to the coroutineScope outlined above, except that a failure in one child does not result in the cancellation of the other coroutines.
- runBlocking – Starts a coroutine and blocks the current thread until the coroutine reaches completion. This is typically the exact opposite of what is wanted from coroutines but is useful for testing code and when integrating legacy code and libraries. Otherwise to be avoided.
Jobs
Each call to a coroutine builder, such as launch or async, returns a Job instance which can, in turn, be used to track and manage the lifecycle of the corresponding coroutine. Subsequent builder calls from within the coroutine create new Job instances, which will become children of the immediate parent Job, forming a parentchild relationship tree where canceling a parent Job will recursively cancel all its children. Canceling a child does not, however, cancel the parent, though an uncaught exception within a child created using the launch builder may result in the cancellation of the parent (this is not the case for children created using the async builder, which encapsulates the exception in the result returned to the parent).
The status of a coroutine can be identified by accessing the isActive, isCompleted, and isCancelled properties of the associated Job object. In addition to these properties, several methods are also available on a Job instance. For example, a Job and all of its children may be canceled by calling the cancel() method of the Job object, while a call to the cancelChildren() method will cancel all child coroutines.
The join() method can be called to suspend the coroutine associated with the job until all of its child jobs have completed. To perform this task and cancel the Job once all child jobs have completed, call the cancelAndJoin() method.
This hierarchical Job structure, together with coroutine scopes, form the foundation of structured concurrency, which aims to ensure that coroutines do not run longer than required without manually keeping references to each coroutine.
Coroutines – Suspending and Resuming
It helps to see some coroutine examples in action to understand coroutine suspension better. To start with, let’s assume a simple Android app containing a button that, when clicked, calls a function named startTask(). This function calls a suspend function named performSlowTask() using the Main coroutine dispatcher. The code for this might read as follows:
private val myCoroutineScope = CoroutineScope(Dispatchers.Main)
fun startTask(view: View) {
myCoroutineScope.launch(Dispatchers.Main) {
performSlowTask()
}
}
Code language: Kotlin (kotlin)
In the above code, a custom scope is declared and referenced in the call to the launch builder, which, in turn, calls the performSlowTask() suspend function. Since startTask() is not a suspend function, the coroutine must be started using the launch builder instead of the async builder.
Next, we can declare the performSlowTask() suspend function as follows:
suspend fun performSlowTask() {
Log.i(TAG, "performSlowTask before")
delay(5_000) // simulates long-running task
Log.i(TAG, "performSlowTask after")
}
Code language: Kotlin (kotlin)
As implemented, all the function does is output diagnostic messages before and after performing a 5-second delay, simulating a long-running task. While the 5-second delay is in effect, the user interface will continue to be responsive because the main thread is not being blocked. To understand why it helps to explore what is happening behind the scenes.
First, the startTask() function is executed and launches the performSlowTask() suspend function as a coroutine. This function then calls the Kotlin delay() function passing through a time value. The built-in Kotlin delay() function is implemented as a suspend function, so it is also launched as a coroutine by the Kotlin runtime environment. The code execution has now reached what is referred to as a suspend point which will cause the performSlowTask() coroutine to be suspended while the delay coroutine is running. This frees up the thread on which performSlowTask() was running and returns control to the main thread so that the UI is unaffected.
Once the delay() function reaches completion, the suspended coroutine will be resumed and restored to a thread from the pool where it can display the Log message and return to the startTask() function.
When working with coroutines in Android Studio suspend points within the code editor are marked as shown in the figure below:
Returning Results from a Coroutine
The above example ran a suspend function as a coroutine but did not demonstrate how to return results. However, suppose the performSlowTask() function is required to return a string value to be displayed to the user via a TextView object.
To do this, we must rewrite the suspend function to return a Deferred object. A Deferred object is a commitment to provide a value at some point in the future. By calling the await() function on the Deferred object, the Kotlin runtime will deliver the value when the coroutine returns it. The code in our startTask() function might, therefore, be rewritten as follows:
fun startTask(view: View) {
coroutineScope.launch(Dispatchers.Main) {
statusText.text = performSlowTask().await()
}
}
Code language: Kotlin (kotlin)
The problem now is that we are having to use the launch builder to start the coroutine since startTask() is not a suspend function. As outlined earlier in this chapter, it is only possible to return results when using the async builder. To get around this, we have to adapt the suspend function to use the async builder to start another coroutine that returns a Deferred result:
suspend fun performSlowTask(): Deferred<String> =
coroutineScope.async(Dispatchers.Default) {
Log.i(TAG, "performSlowTask before")
delay(5_000)
Log.i(TAG, "performSlowTask after")
return@async "Finished"
}
Code language: Kotlin (kotlin)
When the app runs, the “Finished” result string will be displayed on the TextView object when the performSlowTask() coroutine completes. Once again, the wait for the result will occur in the background without blocking the main thread.
Using withContext
As we have seen, coroutines are launched within a specified scope and using a specific dispatcher. By default, any child coroutines will inherit the same dispatcher as that used by the parent. Consider the following code designed to call multiple functions from within a suspend function:
fun startTask(view: View) {
coroutineScope.launch(Dispatchers.Main) {
performTasks()
}
}
suspend fun performTasks() {
performTask1()
performTask2()
performTask3()
}
suspend fun performTask1() {
Log.i(TAG, "Task 1 ${Thread.currentThread().name}")
}
suspend fun performTask2() {
Log.i(TAG, "Task 2 ${Thread.currentThread().name}")
}
suspend fun performTask3 () {
Log.i(TAG, "Task 3 ${Thread.currentThread().name}")
}
Code language: Kotlin (kotlin)
Since the performTasks() function was launched using the Main dispatcher, all three functions will default to the main thread. To prove this, the functions have been written to output the name of the thread in which they are running. On execution, the Logcat panel will contain the following output:
Task 1 main
Task 2 main
Task 3 main
Code language: plaintext (plaintext)
However, imagine that the performTask2() function performs network-intensive operations more suited to the IO dispatcher.. This can easily be achieved using the withContext launcher, which allows the context of a coroutine to be changed while still staying in the same coroutine scope. The following change switches the performTask2() coroutine to an IO thread:
suspend fun performTasks() {
performTask1()
withContext(Dispatchers.IO) { performTask2() }
performTask3()
}
Code language: Kotlin (kotlin)
When executed, the output will read as follows, indicating that the Task 2 coroutine is no longer on the main thread:
Task 1 main
Task 2 DefaultDispatcher-worker-1
Task 3 main
Code language: plaintext (plaintext)
The withContext builder also provides an interesting alternative to using the async builder and the Deferred object await() call when returning a result. Using withContext, the code from the previous section can be rewritten as follows:
fun startTask(view: View) {
coroutineScope.launch(Dispatchers.Main) {
statusText.text = performSlowTask()
}
}
suspend fun performSlowTask(): String =
withContext(Dispatchers.Main) {
Log.i(TAG, "performSlowTask before")
delay(5_000)
Log.i(TAG, "performSlowTask after")
return@withContext "Finished"
}
}
Code language: Kotlin (kotlin)
Coroutine Channel Communication
Channels provide a simple way to implement communication between coroutines, including streams of data. In the simplest form, this involves the creation of a Channel instance and calling the send() method to send the data. Once sent, transmitted data can be received in another coroutine via a call to the receive() method of the same Channel instance.
The following code, for example, passes six integers from one coroutine to another:
.
.
import kotlinx.coroutines.channels.*
.
.
val channel = Channel<Int>()
suspend fun channelDemo() {
coroutineScope.launch(Dispatchers.Main) { performTask1() }
coroutineScope.launch(Dispatchers.Main) { performTask2() }
}
suspend fun performTask1() {
(1..6).forEach {
channel.send(it)
}
}
suspend fun performTask2() {
repeat(6) {
Log.d(TAG, "Received: ${channel.receive()}")
}
}
Code language: Kotlin (kotlin)
When executed, the following logcat output will be generated:
Received: 1
Received: 2
Received: 3
Received: 4
Received: 5
Received: 6
Code language: plaintext (plaintext)
Summary
Kotlin coroutines provide a simpler and more efficient approach to performing asynchronous tasks than traditional multi-threading. Coroutines allow asynchronous tasks to be implemented in a structured way without implementing the callbacks associated with typical thread-based tasks. This chapter has introduced the basic concepts of coroutines, including jobs, scope, builders, suspend functions, structured concurrency, and channel-based communication.