Modern programs rarely only do one thing at a time; networked applications may need to make or serve multiple network requests but shouldn’t wait for each network request to finish before starting the next one. Mobile and desktop apps may need to do anything from querying on-device databases, to using sensors, to communicating with other devices—all while redrawing their user interface 60 times per second or more.
To handle this, modern applications need to do multiple things at the same time, asynchronously. That means developers need tools to allow different operations to execute independently without blocking each other as well as coordinate and synchronize between concurrent tasks.
In this chapter, we’ll take a look at Kotlin’s approach to doing these kinds of asynchronous computations using coroutines. Coroutines allow you to write code that can be both concurrent and parallel.