Introduction to Kotlin Coroutines

Get an overview of Kotlin, Kotlin coroutines, and their importance in our tech stack.

About Kotlin

Kotlin is a general-purpose programming language with the quality of identifying the type of an expression in a formal language. It’s designed to integrate with Java. Kotlin is itself a polyglot language, bringing together powerful capabilities from many different languages. The creators of Kotlin combined the good parts of these languages into one that’s highly approachable and pragmatic.

Kotlin coroutines

Our industry is heading in the direction of using coroutines. Coroutines are like threads in that they take a chunk of code that simultaneously works with the remainder of the code.

Why do we want to learn about Kotlin coroutines?

Common answers to this question include: they’re cool, everyone is talking about them, they’re lighter threads, they’re easier than RxJava, and they allow concurrency while also allowing our code to have an imperative style.

However, coroutines are much more than that. They’re the holy grail of concurrency. As a concept, they’ve been known about in computer science since the 1960s, but in mainstream programming, they’ve only been used in a very limited form (like async/await).

That changed with Golang, which introduced much more general-purpose coroutines. Kotlin built on that, creating what we believe is currently the most powerful and practical implementation of this idea.

Importance

The importance of concurrency is growing, but the classic techniques aren’t enough. Current trends suggest that coroutines are the direction in which our industry is headed, and Kotlin coroutines are a very solid step. Let’s look at some examples to see how well they help in common use cases.