...

/

How Does Suspension Work?

How Does Suspension Work?

Learn about suspension and how it works in Kotlin coroutines.

Suspension: The hallmark of Kotlin coroutines

Suspending functions are the hallmark of Kotlin coroutines. The suspension capability is an essential feature that all other Kotlin coroutines concepts are built on. The following lesson aims to give a solid overview of how they work.

Suspending a coroutine means stopping it in the middle. It's similar to stopping a video game: we save at a checkpoint, turn off the game, and we and our computer focus on doing different things. Then, when we would like to continue sometime later, we turn on the game again, resume from the saved checkpoint, and play from where we previously left off. This is analogous to coroutines. When they're suspended, they return a Continuation. It's like a save in a game—we can use it to continue from the point where we stopped.

Notice that this is very different from a thread, which we cannot save, only block. A coroutine is much more powerful. When suspended, it does not consume any resources. We can resume a coroutine on a different thread, and (at least in theory) a ...