Skip to content
Mobileintermediate

What are Kotlin coroutines?

Updated Nov 30, 20264 min readBased on official documentation
What are Kotlin coroutines?

In short

Kotlin coroutines are a way to run tasks concurrently and write asynchronous code in a sequential style. They are lightweight compared with threads, and they can pause without blocking the thread they run on. The kotlinx.coroutines library provides most of the tools you need to use them.

Why coroutines exist

Modern apps do many things at once. They respond to taps, load data from the network, and update the screen. Doing all of this in a strict order would make the app feel frozen. The usual answer is concurrency, which lets separate operations make progress without waiting for each other.

The classic tool for concurrency is the thread. A thread is an independent path of execution that the operating system manages. Threads work, but they are relatively heavy. Creating many of them costs memory and time, and that can slow an app down.

Kotlin takes a different route. It uses asynchronous programming built around coroutines. A coroutine is a unit of work that can pause and resume later. Because it does not hold a thread while it waits, you can run many of them at once. Coroutines are a good fit for fine-grained concurrency, where you split work into many small tasks.

How it works

The core idea is the suspending function. You mark a function with the suspend keyword. Inside it, you can call other suspending functions that take a long time, such as a network request. When the code reaches such a call, the coroutine pauses. The thread is free to do other work. When the result is ready, the coroutine resumes where it stopped.

The result is code that reads top to bottom, like ordinary sequential code, even though it runs asynchronously. You avoid deeply nested callbacks, which were common in older asynchronous code.

To start a coroutine, you use a coroutine builder. The two most common builders are launch and async. Both are extension functions on CoroutineScope. The scope defines how long the coroutine lives and provides its context.

Every coroutine runs with a context, which is a set of elements that control its behavior. The Job tracks the lifecycle of the coroutine. A CoroutineDispatcher decides which thread or threads the coroutine uses, for example a background thread or the main thread in a UI app. A CoroutineExceptionHandler catches uncaught exceptions. Child coroutines inherit the context of their parent by default. This forms a hierarchy, so related coroutines can be canceled together or share error handling. That idea is called structured concurrency.

When to use it

Use coroutines when your code waits on slow work and should not block the main thread. Typical cases include reading files, calling web APIs, querying a database, and reacting to user input while data loads. In Android apps, coroutines are the standard way to keep the interface responsive.

Coroutines also help when you need several operations to run in parallel and then combine their results. With async, you start multiple tasks, then wait for each result when you need it.

If you are new to the topic, read the Coroutine basics guide first. It walks through suspending functions and builders with small examples. Once those feel natural, move on to composing suspending functions into larger pipelines.

Sharing data between coroutines

Coroutines often need to pass values to each other, and the library gives you several options. Choose based on how the values should be delivered.

A Flow produces values only when a coroutine actively collects them. It suits streams of data that a consumer pulls at its own pace. A Channel lets several coroutines send and receive values, and each value reaches exactly one receiver. A SharedFlow sends every value to all active collectors at once.

Shared mutable state needs extra care. If several coroutines change the same data without coordination, you can get race conditions. These are bugs where operations interfere with each other in unpredictable ways. A StateFlow wraps the shared value. One coroutine updates it, and others collect its latest value. This keeps the data easy to reason about. If you work with Android screens, the MVVM architecture guide on our site shows how such state often fits into a ViewModel.

Common mistakes

Beginners often launch coroutines in a scope that outlives the work they do. The coroutine then keeps running after the user has left the screen. Tie coroutines to a scope with a clear lifetime, so cancellation happens when it should.

Another mistake is doing heavy blocking work on the main thread inside a coroutine. A suspending function only avoids blocking when the code it calls is actually suspending. Choose the right dispatcher for CPU-heavy or file work instead of assuming the coroutine will move the work off the main thread by itself.

Finally, do not treat coroutines as a free replacement for every thread. They are lightweight, but the underlying threads still do the real work. Understanding dispatchers helps you see where your code actually runs.

Alternatives

Before coroutines, developers used callbacks, futures, and reactive libraries. These approaches still exist and work well in many projects. Coroutines, however, let you write asynchronous logic in the same style as normal code, which many developers find easier to read and test. Most coroutine features come from the kotlinx.coroutines library, and its API reference documents every builder, dispatcher and utility in detail. Flutter developers face a similar goal of responsive apps, and our Flutter page describes a different approach to building interfaces.

Where to go next

After the basics, the Guide to UI programming with coroutines explains coroutine-based interface work. The Coroutines and channels tutorial and the Flows documentation go deeper into streams and shared values. For debugging, IntelliJ IDEA has built-in tools for coroutines.

FAQ

Are coroutines the same as threads?

No. Coroutines are lightweight units of work that can pause and resume without blocking a thread. Threads are managed by the operating system and are heavier to create. Coroutines still run on threads, which is why dispatchers matter.

What does the suspend keyword do?

It marks a function that can pause while it waits for a long operation, such as a network call. The thread is released during the pause, so other work can continue. The function resumes when the result is ready.

When should I use launch and when should I use async?

Use launch when you want to start work and do not need a returned value. Use async when you want a result, which you retrieve later with await. Both builders are extension functions on CoroutineScope.

What is the difference between Flow, Channel and SharedFlow?

Flow produces values only when a collector asks for them. A Channel delivers each value to exactly one receiver. SharedFlow sends every value to all active collectors.

Sources

This page is based on the official documentation below. Read it for full detail.

Kotlin coroutines — official documentation ↗

Kotlin coroutines in the news

blozhikDeveloper news from across the web, explained. Every story links to its original sources.