Running Concurrently Using Coroutines
We'll cover the following...
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.
fun task1() {println("start task1 in Thread ${Thread.currentThread()}")println("end task1 in Thread ${Thread.currentThread()}")}fun task2() {println("start task2 in Thread ${Thread.currentThread()}")println("end task2 in Thread ${Thread.currentThread()}")}println("start")run {task1()task2()println("called task1 and task2 from ${Thread.currentThread()}")}println("done")
Each of the two functions task1()
and task2()
print the details about their thread of execution upon entry and before exit. The run()
function is an extension function of the Any
class. We invoke run
and pass a lambda expression as argument to it. Within the lambda we call the two functions and then print a message that we’ve invoked the functions.
Here’s the output from the code:
start
start task1 in Thread Thread[main,5,main]
end task1 in Thread Thread[main,5,main]
start task2 in Thread Thread[main,5,main]
end task2 in Thread Thread[main,5,main]
called task1 and task2 from Thread[main,5,main]
done
The function calls are executing sequentially, like we expect common programs to do, with task1()
completing, then task2()
starting and then running to completion, and, finally, the last line within the lambda passed to run()
executing. Nothing new there, but the code serves as a starting point for playing with coroutines.
Creating a coroutine
In the previous example, the main thread executed sequentially the code ...