We use delays to pause actions in programs. Delay is for suspending without blocking, while Thread.sleep blocks the thread completely.
Delay vs Thread.sleep in Kotlin
Introduction
Syntax
Kotlin
suspend fun delay(timeMillis: Long) fun Thread.sleep(timeMillis: Long)
delay is a suspend function used inside coroutines.
Thread.sleep is a blocking call that stops the current thread.
Examples
delay inside a coroutine. It pauses without blocking the thread.Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { println("Start delay") delay(1000L) // pauses coroutine for 1 second println("End delay") }
Thread.sleep which blocks the whole thread for 1 second.Kotlin
fun main() { println("Start sleep") Thread.sleep(1000) // blocks thread for 1 second println("End sleep") }
Sample Program
This program shows both delay and Thread.sleep. The first pauses inside a coroutine without blocking. The second blocks the thread.
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { println("Before delay") delay(500L) println("After delay") } fun blockingSleep() { println("Before sleep") Thread.sleep(500) println("After sleep") } fun mainBlocking() { blockingSleep() } // Run coroutine example main() // Run blocking example mainBlocking()
Important Notes
delay requires a coroutine context to work.
Thread.sleep can freeze your app if used on the main thread.
Use delay for smooth, responsive apps and Thread.sleep for simple blocking waits.
Summary
delay pauses coroutines without blocking threads.
Thread.sleep blocks the current thread completely.
Prefer delay in asynchronous Kotlin code for better performance.
Practice
1. What is the main difference between
delay and Thread.sleep in Kotlin?easy
Solution
Step 1: Understand
delaybehaviordelaysuspends the coroutine but does not block the underlying thread, allowing other coroutines to run.Step 2: Understand
Thread.sleepbehaviorThread.sleepblocks the entire thread, stopping all work on that thread until the sleep time ends.Final Answer:
delaypauses a coroutine without blocking the thread, whileThread.sleepblocks the thread. -> Option BQuick Check:
delay = non-blocking pause, Thread.sleep = blocking pause [OK]
Hint: delay suspends coroutines; Thread.sleep blocks threads [OK]
Common Mistakes:
- Thinking delay blocks the thread
- Confusing coroutine suspension with thread blocking
- Assuming both behave the same way
2. Which of the following is the correct way to use
delay in a Kotlin coroutine?easy
Solution
Step 1: Recall
delaysyntaxdelayis a suspend function called with the time in milliseconds as an argument, e.g.,delay(1000).Step 2: Identify incorrect options
Thread.delayanddelay.sleepdo not exist.Thread.sleepis a different blocking function.Final Answer:
delay(1000) -> Option DQuick Check:
Correct syntax for delay = delay(1000) [OK]
Hint: Use delay(milliseconds) inside coroutines only [OK]
Common Mistakes:
- Using Thread.delay instead of delay
- Confusing delay with Thread.sleep syntax
- Calling delay outside coroutine scope
3. What will be the output of this Kotlin coroutine code snippet?
import kotlinx.coroutines.*
fun main() = runBlocking {
println("Start")
delay(500)
println("Middle")
Thread.sleep(500)
println("End")
}medium
Solution
Step 1: Analyze the coroutine flow
The program prints "Start", then suspends for 500ms usingdelay, then prints "Middle".Step 2: Analyze the thread blocking
After "Middle",Thread.sleep(500)blocks the thread for 500ms, then prints "End".Final Answer:
Start Middle End (with 1 second total delay) -> Option AQuick Check:
delay + Thread.sleep = total 1 second pause [OK]
Hint: delay suspends coroutine; Thread.sleep blocks thread sequentially [OK]
Common Mistakes:
- Assuming delay and Thread.sleep run in parallel
- Thinking delay blocks the thread
- Confusing output order
4. Identify the problem in this Kotlin coroutine code and choose the fix:
import kotlinx.coroutines.*
fun main() = runBlocking {
println("Before sleep")
Thread.sleep(1000)
println("After sleep")
}medium
Solution
Step 1: Identify blocking call in coroutine
Thread.sleep(1000)blocks the thread, which is not recommended inside coroutines.Step 2: Suggest coroutine-friendly alternative
Usedelay(1000)to suspend the coroutine without blocking the thread.Final Answer:
ReplaceThread.sleep(1000)withdelay(1000)to avoid blocking the thread. -> Option CQuick Check:
Use delay, not Thread.sleep, inside coroutines [OK]
Hint: Avoid Thread.sleep in coroutines; use delay instead [OK]
Common Mistakes:
- Using Thread.sleep inside coroutines
- Trying to use Thread.delay which doesn't exist
- Ignoring thread blocking effects
5. You want to run two tasks concurrently in Kotlin coroutines, each pausing for 1 second. Which approach uses
delay correctly to avoid blocking threads and runs tasks concurrently?
fun main() = runBlocking {
// Choose the correct code snippet
}hard
Solution
Step 1: Understand concurrency with coroutines
Usinglaunchstarts concurrent coroutines. Inside them,delaysuspends without blocking threads.Step 2: Analyze options for concurrency and blocking
launch { delay(1000); println("Task 1 done") } launch { delay(1000); println("Task 2 done") } usesdelayinsidelaunch, enabling concurrency. launch { Thread.sleep(1000); println("Task 1 done") } launch { Thread.sleep(1000); println("Task 2 done") } blocks threads withThread.sleep. Thread.sleep(1000); println("Task 1 done") Thread.sleep(1000); println("Task 2 done") blocks main thread sequentially. delay(1000); println("Task 1 done") delay(1000); println("Task 2 done") callsdelayoutside coroutine scope, causing error.Final Answer:
launch { delay(1000); println("Task 1 done") } launch { delay(1000); println("Task 2 done") } -> Option AQuick Check:
Use launch + delay for concurrent non-blocking tasks [OK]
Hint: Use launch with delay for concurrency, not Thread.sleep [OK]
Common Mistakes:
- Using Thread.sleep inside launch blocks threads
- Calling delay outside coroutine scope
- Running tasks sequentially blocking main thread
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
