Mobile Development 13 min read

Understanding Kotlin Coroutine Suspension: launch, async, and suspend Explained

This article explains how Kotlin coroutines work in Android, comparing launch and async, detailing the role of suspend functions, thread switching with Dispatchers, and how the coroutine framework handles suspension and resumption to simplify asynchronous programming.

Jike Tech Team
Jike Tech Team
Jike Tech Team
Understanding Kotlin Coroutine Suspension: launch, async, and suspend Explained

Recap of the previous episode

In the last episode we covered:

What a coroutine actually is

Why coroutines are useful

How to use coroutines

Most of the time we create coroutines with the

launch

function, but two other functions can also be used:

runBlocking

– mainly for unit tests because it blocks the thread.

async

– returns a

Deferred

result.

We will now compare

launch

and

async

:

Both start a coroutine and return a

Coroutine

object.

async

additionally implements the

Deferred

interface.

Deferred

represents a value that will be available later; you obtain it with

Deferred.await()

.

What "suspend" really does

When a coroutine reaches a

suspend

function, the coroutine is paused and detached from the thread that was executing it.

The thread continues its own work (e.g., UI refresh on the main thread or background tasks on a pool thread) while the coroutine is suspended.

After the

suspend

function completes, the coroutine automatically switches back to the original thread and resumes execution.

This thread‑switching is performed by the coroutine framework, not by the

suspend

keyword itself. The keyword merely signals that the function is potentially long‑running and must be called from a coroutine.

How suspension is implemented

Suspension happens when a

suspend

function internally calls another suspend function such as

withContext

or

delay

. For example:

<code>suspend fun suspendingGetImage(id: String) = withContext(Dispatchers.IO) {
    // IO work here
}</code>
withContext

receives a

Dispatcher

(e.g.,

Dispatchers.IO

) that tells the coroutine which thread pool to switch to while the work is performed.

Simply adding the

suspend

modifier without any real suspension logic results in a compiler warning "redundant suspend modifier" because the function does not actually cause a thread switch.

Custom suspend functions

To create a custom suspend function, add the

suspend

keyword and wrap the body with a real suspending call like

withContext

or

delay

:

<code>suspend fun suspendingPrint() {
    println("Thread: ${Thread.currentThread().name}")
}</code>

Or a function that waits:

<code>suspend fun suspendUntilDone() {
    while (!done) {
        delay(5)
    }
}</code>

These patterns let the coroutine framework handle the actual thread switching and resumption.

Summary

Suspension in Kotlin coroutines is a thread‑scheduling operation that temporarily detaches a coroutine from its current thread, runs the suspend block on the dispatcher‑specified thread, and then automatically switches back to continue execution.

The

suspend

keyword itself is only a compile‑time reminder that the function must be called from a coroutine.

Next time we will answer common questions such as how non‑blocking suspension works and how coroutines compare to RxJava.

Mobile DevelopmentAndroidConcurrencyKotlinAsynccoroutinessuspendlaunch
Jike Tech Team
Written by

Jike Tech Team

Article sharing by the Jike Tech Team

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.