Talk: Using Kotlin Coroutines for Asynchronous and Concurrent Programming
Asynchronous and concurrent programming plays a very important role in the current world of HTTP APIs and microservices, where a significant part of our code is about orchestrating interactions with external systems.
There are various programming models to handle asynchronicity, ranging from simple callbacks to reactive streams, as a way to handle asynchronous sequences.
However, instead of also adding explicit async-await support in the Kotlin language, their designers decided to go another route and address these problems with the different and more generic concept of coroutines.
This session presents Kotlin coroutines, as implemented via suspending functions, and their use to write asynchronous and concurrent programs.
Starting from the ground-up, we show how suspending functions allows us to turn callbacks into suspension points that don't break the apparent control flow.
From then we move into creating, starting coroutines as instances of these suspending functions. We show how the async-await construct can be implemented as library functions without needing explicit language support.
Special emphasis is given to the concept of structured concurrency as a way to manage and coordinate coroutines, namely its cancellation.
The relation between coroutines and threads is also illustrated, with emphasis on thread scheduling (e.g. ensuring the coroutine always run in the Android UI thread) and context propagation.