Have you ever tried to create a multithreaded application capable of handling all your user requests at the same time, but ended up juggling threads and thread pools, ultimately wondering whether it was worth it in the end? Writing asynchronous or concurrent code has always been notoriously hard. It is difficult to reason about the execution of the code and requires a lot of boilerplate. Luckily, there is an easier way! Coroutines provide a more intuitive way of writing code that can perform several tasks at once, without much of the usual hassle of dealing with blocking threads and thread pools.
In this talk, I will introduce you to Kotlin Coroutines, a library for writing asynchronous code, developed by JetBrains. The concept of suspending functions is explained, and you will get a firm grasp of what you can do with Kotlin Coroutines and why you should use them, but more importantly how to use them the way they are intended. You will also learn why it is possible to run thousands of coroutines simultaneously, without creating as many threads!
Furthermore, the concept of non-blocking I/O will be explained by creating a fully reactive web application implemented using Spring WebFlux and R2DBC (reactive JDBC), where Kotlin Coroutines are used as the ‘glue’ between the incoming HTTP requests and the calls to the database, with as little boilerplate code as possible.