0
0
Kotlinprogramming~10 mins

Delay vs Thread.sleep in Kotlin - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Delay vs Thread.sleep
Start Coroutine
Call delay(1000)
Coroutine Suspends
Other coroutines run
After 1 second
Coroutine Resumes
Continue execution
Start Thread
Call Thread.sleep(1000)
Thread Blocks
No other code runs in this thread
After 1 second
Thread wakes up
Continue execution
Shows how delay suspends a coroutine allowing others to run, while Thread.sleep blocks the thread stopping all work on it.
Execution Sample
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
  println("Start")
  delay(1000)
  println("End")
}
This code prints Start, waits 1 second without blocking, then prints End.
Execution Table
StepActionStateOutput
1Start runBlocking coroutineCoroutine activePrints 'Start'
2Call delay(1000)Coroutine suspends, thread freeNo output
3Other coroutines or main thread can runCoroutine suspendedNo output
4After 1 second delay endsCoroutine resumesNo output
5Print 'End'Coroutine activePrints 'End'
6Coroutine completesCoroutine finishedNo output
💡 Coroutine finishes after printing 'End'
Variable Tracker
VariableStartAfter delayAfter resumeFinal
Coroutine StateActiveSuspendedActiveFinished
Key Moments - 3 Insights
Why does delay not block the whole program?
Because delay suspends only the coroutine, freeing the thread to run other coroutines or tasks, as shown in execution_table step 3.
What happens to the thread during Thread.sleep?
The thread is blocked and cannot do any other work until sleep finishes, unlike delay which suspends coroutine but keeps thread free.
Can delay be used outside coroutines?
No, delay is a suspend function and must be called inside a coroutine or another suspend function.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the coroutine state right after calling delay(1000)?
AActive
BSuspended
CFinished
DBlocked
💡 Hint
Check execution_table row 2 under 'State' column
At which step does the coroutine resume after delay?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for 'Coroutine resumes' in execution_table
If Thread.sleep was used instead of delay, what would happen to the thread?
AThread would block and no other code runs on it
BThread would suspend but allow other coroutines
CThread would continue running other coroutines
DThread would crash
💡 Hint
Refer to concept_flow section about Thread.sleep behavior
Concept Snapshot
delay(1000) suspends coroutine without blocking thread
Thread.sleep(1000) blocks the thread completely
Use delay inside coroutines for non-blocking waits
Thread.sleep blocks all work on the thread
delay allows other coroutines to run during wait
Full Transcript
This visual execution compares delay and Thread.sleep in Kotlin. delay suspends a coroutine, freeing the thread to run other coroutines, shown by the coroutine state changing to suspended and resuming after 1 second. Thread.sleep blocks the entire thread, stopping all work on it until the sleep ends. The example code runs a coroutine that prints 'Start', delays 1 second without blocking, then prints 'End'. Key points: delay is non-blocking and must be used inside coroutines; Thread.sleep blocks the thread and should be avoided in coroutines.