Deep Kotlin tutorial covering suspending, blocking, continuation, state machine with examples, diagram, mistakes, and interview answers.
Suspending vs Blocking, Continuations, and ANR Risks
What is Suspending vs Blocking, Continuations, and ANR Risks?
Suspending vs Blocking, Continuations, and ANR Risks is an important Kotlin topic from 2.2 Suspending vs Blocking. In this lesson we focus only on the concepts that belong together: suspending, blocking, continuation, state machine, delay, Thread.sleep, ANR. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.
Why students should learn it
- Understand Suspending vs Blocking, Continuations, and ANR Risks in simple language
- Use suspending in real Kotlin or Android code
- Explain Suspending vs Blocking, Continuations, and ANR Risks in interviews with examples and trade-offs
How it works
Suspending vs Blocking, Continuations, and ANR Risks: suspending, blocking, continuation, state machine, delay, Thread.sleep, ANR works by giving asynchronous work a clear owner and execution rule. In real Android code, first identify who owns the work, then choose the dispatcher, then decide how cancellation and errors should behave. This prevents background work from continuing after the screen or ViewModel no longer needs it.
- Identify what problem Suspending vs Blocking, Continuations, and ANR Risks: suspending, blocking, continuation, state machine, delay, Thread.sleep, ANR solves.
- Write the smallest Kotlin snippet that demonstrates it.
- Add one Android or JVM use case where it matters.
- Compare it with a similar concept so the difference is clear.
- Finish by naming one common mistake and the safer alternative.
Interview tip: keep the answer simple first, then add example, trade-off, and mistake.
Types / Classification
- suspending: core concept in this lesson
- blocking: core concept in this lesson
- continuation: core concept in this lesson
- state machine: core concept in this lesson
- delay: core concept in this lesson
- Thread.sleep: core concept in this lesson
- ANR: core concept in this lesson
- Process: isolated running program with its own memory
Important differences
| Concept 1 | Concept 2 | How to remember it |
|---|---|---|
| suspend | blocking | suspend pauses coroutine; blocking occupies a thread. |
| launch | async | launch returns Job; async returns Deferred result. |
| scope | dispatcher | scope owns lifetime; dispatcher chooses execution thread. |
Diagram
flowchart TD
A["2.2 Suspending vs Blocking"] --> B["Suspending vs Blocking, Continuations, and ANR Risks"]
B --> C["Why it exists"]
C --> D["How it works in Kotlin"]
D --> E["Android or JVM example"]
E --> F["Mistakes and interview answer"]
Examples
Unsynchronized counter (race condition)
var counter = 0
val threads = List(100) { Thread { counter++ } }
threads.forEach { it.start() }
threads.forEach { it.join() }
// counter is often less than 100 here
Fixing it with synchronization
val lock = Any()
var counter = 0
fun increment() {
synchronized(lock) { counter++ }
}
When to use it
Use it when: Use coroutines for asynchronous work in an Android app or backend service: network calls, database reads, file I/O, or anything that would otherwise block a thread - for example loading a user's profile from an API while keeping the UI responsive.
Avoid it when: Avoid launching unstructured coroutines with GlobalScope in app code, and avoid coroutines for pure, fast, synchronous logic where a plain function call is simpler.
Common mistakes
- Using GlobalScope for screen work
- Blocking Dispatchers.Main
- Catching Exception and swallowing CancellationException
- Using runBlocking in Android UI code
- Launching work without clear ownership
Interview questions
Read each answer in four parts: simple answer, code example, trade-off, and common mistake.
1. What is Suspending vs Blocking, Continuations, and ANR Risks in 2.2 Suspending vs Blocking?
Answer: Suspending vs Blocking, Continuations, and ANR Risks is an important Kotlin topic from 2.2 Suspending vs Blocking. In this lesson we focus only on the concepts that belong together: suspending, blocking, continuation, state machine, delay, Thread.sleep, ANR. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.
var counter = 0
val threads = List(100) { Thread { counter++ } }
threads.forEach { it.start() }
threads.forEach { it.join() }
// counter is often less than 100 here- Trade-off: suspend and blocking solve related but different problems: suspend pauses coroutine; blocking occupies a thread.
- Common mistake: Using GlobalScope for screen work
2. How does Suspending vs Blocking, Continuations, and ANR Risks work in Kotlin or Android code?
Answer: Suspending vs Blocking, Continuations, and ANR Risks: suspending, blocking, continuation, state machine, delay, Thread.sleep, ANR works by giving asynchronous work a clear owner and execution rule. In real Android code, first identify who owns the work, then choose the dispatcher, then decide how cancellation and errors should behave. This prevents background work from continuing after the screen or ViewModel no longer needs it.
val lock = Any()
var counter = 0
fun increment() {
synchronized(lock) { counter++ }
}- Trade-off: suspend and blocking solve related but different problems: suspend pauses coroutine; blocking occupies a thread.
- Common mistake: Blocking Dispatchers.Main
3. What is one common mistake with Suspending vs Blocking, Continuations, and ANR Risks?
Answer: A common mistake is: Using GlobalScope for screen work Another is: Blocking Dispatchers.Main
var counter = 0
val threads = List(100) { Thread { counter++ } }
threads.forEach { it.start() }
threads.forEach { it.join() }
// counter is often less than 100 here- Trade-off: suspend and blocking solve related but different problems: suspend pauses coroutine; blocking occupies a thread.
- Common mistake: Using GlobalScope for screen work
4. How would you explain Suspending vs Blocking, Continuations, and ANR Risks with a small example?
Answer: Suspending vs Blocking, Continuations, and ANR Risks is an important Kotlin topic from 2.2 Suspending vs Blocking. In this lesson we focus only on the concepts that belong together: suspending, blocking, continuation, state machine, delay, Thread.sleep, ANR. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas. Here is a short example that shows it in practice.
var counter = 0
val threads = List(100) { Thread { counter++ } }
threads.forEach { it.start() }
threads.forEach { it.join() }
// counter is often less than 100 here- Trade-off: suspend and blocking solve related but different problems: suspend pauses coroutine; blocking occupies a thread.
- Common mistake: Blocking Dispatchers.Main
5. What is the trade-off of using Suspending vs Blocking, Continuations, and ANR Risks?
Answer: suspend and blocking solve related but different problems: suspend pauses coroutine; blocking occupies a thread.
val lock = Any()
var counter = 0
fun increment() {
synchronized(lock) { counter++ }
}- Trade-off: suspend and blocking solve related but different problems: suspend pauses coroutine; blocking occupies a thread.
- Common mistake: Using GlobalScope for screen work
Practice task
Write one Kotlin example for Suspending vs Blocking, Continuations, and ANR Risks. Then explain it using: what is it, how it works, where it is used, mistake to avoid, and one interview answer.