Build Stuff 2018 has ended
Back To Schedule
Thursday, November 15 • 10:20am - 11:10am
[SLIDES]Pedro Felix @pmhsfelix - Using Kotlin Coroutines for Asynchronous and Concurrent Programming

Sign up or log in to save this to your schedule, view media, leave feedback and see who's attending!

Feedback form is now closed.
Asynchronous and concurrent programming play a very important role in the current world of Web APIs and microservices, where a significant part of our code is about orchestrating interactions with external systems.
Using traditional synchronous models, where threads are blocked while waiting for external responses, is not suitable for platforms where threads are costly, such as .NET or the JVM, or where there are special threads that can't be blocked, such as on Javascript or Android applications.

There are various programming models to handle asynchronicity, ranging from simple callbacks to reactive streams as a way to handle asynchronous sequences.
Among these, the concept of Future has seen broad adoption in multiple platforms (e.g. Promises/thenables in javascript, CompletableFuture in Java 8, Task in .NET), including language support via the async-await constructs, which are now available in languages such as C#, Javascript, and Python.
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 and starting coroutines as instances of these suspending functions, taking a look at the underlying state machine and continuation interfaces.
With this knowledge, we show how the async-await construct can be implemented as library functions without needing explicit language support.
We also show how to achieve interoperability with other JVM asynchronous constructs, converting between them and coroutines.
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.

With the recent release of Kotlin 1.3, coroutines graduated to a stable feature, becoming an essential language mechanism to write asynchronous code on the Kotlin language.

avatar for Pedro Felix

Pedro Felix

Software Engineer and Professor, Lisbon Polytechnical Institute
Pedro Félix is a software engineer working in sunny Lisbon, where he deploys both code and knowledge.He's also a professor at the Lisbon Polytechnical Institute, where he teaches Web Application Development and Concurrent Programming courses.Pedro started his professional career... Read More →

Thursday November 15, 2018 10:20am - 11:10am EET
5. Zeta