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.
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
launchfunction, but two other functions can also be used:
runBlocking– mainly for unit tests because it blocks the thread.
async– returns a
Deferredresult.
We will now compare
launchand
async:
Both start a coroutine and return a
Coroutineobject.
asyncadditionally implements the
Deferredinterface.
Deferredrepresents a value that will be available later; you obtain it with
Deferred.await().
What "suspend" really does
When a coroutine reaches a
suspendfunction, 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
suspendfunction 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
suspendkeyword 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
suspendfunction internally calls another suspend function such as
withContextor
delay. For example:
<code>suspend fun suspendingGetImage(id: String) = withContext(Dispatchers.IO) {
// IO work here
}</code> withContextreceives a
Dispatcher(e.g.,
Dispatchers.IO) that tells the coroutine which thread pool to switch to while the work is performed.
Simply adding the
suspendmodifier 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
suspendkeyword and wrap the body with a real suspending call like
withContextor
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
suspendkeyword 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.
Jike Tech Team
Article sharing by the Jike Tech Team
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.