📖 Scenario: You are building a simple Kotlin program to understand how to pause execution in two different ways: using Thread.sleep and delay from coroutines. This helps you see how blocking and non-blocking pauses work in real programs.
🎯 Goal: Create a Kotlin program that first pauses using Thread.sleep and then pauses using delay inside a coroutine. You will see the difference in how the program behaves.
📋 What You'll Learn
Create a main function
Use Thread.sleep to pause for 1000 milliseconds
Use delay to pause for 1000 milliseconds inside a coroutine
Print messages before and after each pause to observe the flow
💡 Why This Matters
🌍 Real World
Understanding blocking vs non-blocking pauses is important when writing apps that need to stay responsive, like games or user interfaces.
💼 Career
Many Kotlin jobs require knowledge of coroutines and how to manage delays without freezing the app, improving user experience.
Progress0 / 4 steps
1
Create the main function and print start message
Write a main function and inside it print "Program started".
Kotlin
Hint
Use fun main() to create the main function and println to print the message.
2
Add Thread.sleep to pause for 1000 milliseconds
Inside the main function, after printing "Program started", add Thread.sleep(1000) to pause the program for 1000 milliseconds, then print "After Thread.sleep".
Kotlin
Hint
Use Thread.sleep(1000) to pause for 1 second, then print the message after it.
3
Add coroutine with delay to pause without blocking
Import kotlinx.coroutines.*. Inside main, add runBlocking block. Inside it, print "Before delay", then call delay(1000), then print "After delay".
Kotlin
Hint
Use runBlocking to start a coroutine in main. Inside it, use delay(1000) to pause without blocking the thread.
4
Print final message after coroutine block
After the runBlocking block in main, print "Program ended" to show the program finished.
Kotlin
Hint
Print "Program ended" after the coroutine finishes to see the full flow.
Practice
(1/5)
1. What is the main difference between delay and Thread.sleep in Kotlin?
easy
A. Both delay and Thread.sleep block the thread in the same way.
B. delay pauses a coroutine without blocking the thread, while Thread.sleep blocks the thread.
C. delay blocks the thread, while Thread.sleep pauses a coroutine without blocking.
D. delay is used only for UI updates, Thread.sleep is for background tasks.
Solution
Step 1: Understand delay behavior
delay suspends the coroutine but does not block the underlying thread, allowing other coroutines to run.
Step 2: Understand Thread.sleep behavior
Thread.sleep blocks the entire thread, stopping all work on that thread until the sleep time ends.
Final Answer:
delay pauses a coroutine without blocking the thread, while Thread.sleep blocks the thread. -> Option B
A. Change Thread.sleep(1000) to Thread.delay(1000) for coroutine compatibility.
B. Add runBlocking inside the main function to fix the error.
C. Replace Thread.sleep(1000) with delay(1000) to avoid blocking the thread.
D. No problem; the code is correct as is.
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
Use delay(1000) to suspend the coroutine without blocking the thread.
Final Answer:
Replace Thread.sleep(1000) with delay(1000) to avoid blocking the thread. -> Option C
Quick 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
}