Bird
Raised Fist0
Kotlinprogramming~10 mins

Delay vs Thread.sleep in Kotlin - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to pause the coroutine without blocking the thread.

Kotlin
suspend fun pause() {
    [1](1000L)
}
Drag options to blanks, or click blank then click option'
AThread.sleep
Bdelay
Cwait
Dsleep
Attempts:
3 left
💡 Hint
Common Mistakes
Using Thread.sleep blocks the thread and is not recommended in coroutines.
2fill in blank
medium

Complete the code to block the current thread for 500 milliseconds.

Kotlin
fun blockThread() {
    [1](500)
}
Drag options to blanks, or click blank then click option'
AThread.sleep
Bdelay
Cwait
Dpause
Attempts:
3 left
💡 Hint
Common Mistakes
Using delay in a non-suspending function causes errors.
3fill in blank
hard

Fix the error in the coroutine by replacing the blocking call with a suspending function.

Kotlin
suspend fun waitOneSecond() {
    [1](1000L)
}
Drag options to blanks, or click blank then click option'
Asleep
Bwait
Cdelay
DThread.sleep
Attempts:
3 left
💡 Hint
Common Mistakes
Using Thread.sleep inside suspend functions blocks the thread.
4fill in blank
hard

Fill both blanks to create a coroutine that prints before and after a non-blocking delay.

Kotlin
suspend fun printWithDelay() {
    println("Start")
    [1](2000L)
    println([2])
}
Drag options to blanks, or click blank then click option'
Adelay
B"End"
C"Done"
DThread.sleep
Attempts:
3 left
💡 Hint
Common Mistakes
Using Thread.sleep blocks the thread and delays printing.
5fill in blank
hard

Fill all three blanks to create a coroutine that delays, then blocks the thread, and finally prints a message.

Kotlin
suspend fun mixedWait() {
    [1](1000L)
    [2](500)
    println([3])
}
Drag options to blanks, or click blank then click option'
Adelay
BThread.sleep
C"Finished"
Dwait
Attempts:
3 left
💡 Hint
Common Mistakes
Using blocking calls before suspending functions can cause unexpected delays.

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

  1. Step 1: Understand delay behavior

    delay suspends the coroutine but does not block the underlying thread, allowing other coroutines to run.
  2. Step 2: Understand Thread.sleep behavior

    Thread.sleep blocks the entire thread, stopping all work on that thread until the sleep time ends.
  3. Final Answer:

    delay pauses a coroutine without blocking the thread, while Thread.sleep blocks the thread. -> Option B
  4. Quick 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
A. Thread.delay(1000)
B. Thread.sleep(1000)
C. delay.sleep(1000)
D. delay(1000)

Solution

  1. Step 1: Recall delay syntax

    delay is a suspend function called with the time in milliseconds as an argument, e.g., delay(1000).
  2. Step 2: Identify incorrect options

    Thread.delay and delay.sleep do not exist. Thread.sleep is a different blocking function.
  3. Final Answer:

    delay(1000) -> Option D
  4. Quick 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
A. Start Middle End (with 1 second total delay)
B. Start End Middle (order switched)
C. Start Middle End (with 0.5 second total delay)
D. Start End (missing Middle)

Solution

  1. Step 1: Analyze the coroutine flow

    The program prints "Start", then suspends for 500ms using delay, then prints "Middle".
  2. Step 2: Analyze the thread blocking

    After "Middle", Thread.sleep(500) blocks the thread for 500ms, then prints "End".
  3. Final Answer:

    Start Middle End (with 1 second total delay) -> Option A
  4. Quick 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
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

  1. Step 1: Identify blocking call in coroutine

    Thread.sleep(1000) blocks the thread, which is not recommended inside coroutines.
  2. Step 2: Suggest coroutine-friendly alternative

    Use delay(1000) to suspend the coroutine without blocking the thread.
  3. Final Answer:

    Replace Thread.sleep(1000) with delay(1000) to avoid blocking the thread. -> Option C
  4. 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
}
hard
A. launch { delay(1000); println("Task 1 done") } launch { delay(1000); println("Task 2 done") }
B. launch { Thread.sleep(1000); println("Task 1 done") } launch { Thread.sleep(1000); println("Task 2 done") }
C. Thread.sleep(1000); println("Task 1 done") Thread.sleep(1000); println("Task 2 done")
D. delay(1000); println("Task 1 done") delay(1000); println("Task 2 done")

Solution

  1. Step 1: Understand concurrency with coroutines

    Using launch starts concurrent coroutines. Inside them, delay suspends without blocking threads.
  2. Step 2: Analyze options for concurrency and blocking

    launch { delay(1000); println("Task 1 done") } launch { delay(1000); println("Task 2 done") } uses delay inside launch, enabling concurrency. launch { Thread.sleep(1000); println("Task 1 done") } launch { Thread.sleep(1000); println("Task 2 done") } blocks threads with Thread.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") calls delay outside coroutine scope, causing error.
  3. Final Answer:

    launch { delay(1000); println("Task 1 done") } launch { delay(1000); println("Task 2 done") } -> Option A
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard