CHAPTER 23
Beginner
Coroutines in Kotlin
Updated: May 18, 2026
5 min read
# CHAPTER 23
Coroutines in Kotlin
1. Chapter Introduction
If you build an Android app and make a network request to download an image on the Main UI Thread, the app will freeze. If the download takes 5 seconds, the user cannot scroll or click for 5 seconds. The OS will likely crash the app with an "ANR" (Application Not Responding) error. To fix this, we must run heavy tasks in the background. Traditionally, this was done using OS Threads, which are heavy and complex. Kotlin solves this elegantly with Coroutines—lightweight "green threads" that make asynchronous programming as simple as writing sequential code.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the difference between OS Threads and Coroutines.
-
Define and call
suspendfunctions.
-
Launch background tasks using
GlobalScope.launch.
-
Return data from background tasks using
asyncandawait.
-
Use
delay()to yield execution.
3. What is a Coroutine?
A Coroutine is a concurrency design pattern. Think of it as a very lightweight thread. You can run 100,000 coroutines simultaneously on a single standard CPU without crashing your computer because they do not map 1:1 to heavy OS threads. They "suspend" (pause) when waiting for data, freeing the underlying thread to do other work!*Note: Coroutines require an external library. In a Gradle project, you add org.jetbrains.kotlinx:kotlinx-coroutines-core.*
4. The launch Builder
The simplest way to start a coroutine is launch. It follows the "fire and forget" principle—it starts a background task and doesn't return a result.
kotlin
*Output:*
5. Suspend Functions
Asuspend function is a special function that can be paused and resumed later. delay() is a suspend function.
*Rule: Suspend functions can ONLY be called from a coroutine or another suspend function.*
kotlin
6. async and await
If you need to run multiple background tasks simultaneously AND you need them to return data, you use async. async returns a Deferred object (similar to a Future or Promise in other languages). You call .await() to get the result.
kotlin
7. Mini Project: Async Task Runner
kotlin
8. Common Mistakes
-
Using
Thread.sleep()inside a Coroutine:Thread.sleepblocks the actual OS thread, freezing everything.delay()suspends the coroutine, allowing the OS thread to execute other coroutines. Never useThread.sleepinside a coroutine.
-
Calling
suspendfunctions from normal functions: The compiler will throw an error. A suspend function must be inside alaunch,async, orrunBlockingblock.
9. Best Practices
-
Avoid
GlobalScope: In production Android apps, never useGlobalScope.launch. If the user closes the app, a GlobalScope coroutine keeps running in the background, draining the battery! Use lifecycle-aware scopes likeviewModelScopeorlifecycleScope.
10. Exercises
-
1.
Write a
suspend fun calculateScore(): Intthat delays for 2 seconds and returns 100.
-
2.
Inside
fun main() = runBlocking, call it and print the result.
11. MCQs with Answers
Question 1
What is a major problem with running heavy tasks on the Main Thread in Android?
Question 2
What is a Kotlin Coroutine?
Question 3
Which coroutine builder is used for "fire and forget" background tasks that do not return a result?
Question 4
What keyword is required to define a function that can be paused and resumed later?
Question 5
Where is the ONLY place a suspend function can be called?
Question 6
What function is the coroutine equivalent of Thread.sleep(), but safely yields the thread instead of blocking it?
Question 7
If a background task needs to return data, which coroutine builder should you use?
Question 8
What method must you call on the result of an async block to retrieve the actual data?
runBlocking block the main thread?
a) Yes, it bridges regular synchronous code with async coroutine code by blocking the thread until all its internal coroutines finish b) No, it is non-blocking
Answer: a) Yes, it blocks the thread.
Question 10
Why is GlobalScope dangerous in Android development?
12. Interview Questions
- Q: Contrast Coroutines with OS Threads. Why can you spawn 100,000 Coroutines but not 100,000 OS Threads?
-
Q: Explain the difference between
launchandasync.
13. Summary
Coroutines completely modernize asynchronous programming. By removing the "callback hell" associated with Java threads and RxJava, Kotlin allows developers to write async network calls and database queries in a sequential, top-to-bottom style usingsuspend and await. It is the definitive way to handle concurrency on the JVM.