0
0
Kotlinprogramming~20 mins

Delay vs Thread.sleep in Kotlin - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Coroutine Delay Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output difference between delay and Thread.sleep in coroutines

What will be the output of this Kotlin coroutine code snippet?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        println("Start delay")
        delay(100)
        println("End delay")
    }
    launch {
        println("Start sleep")
        Thread.sleep(100)
        println("End sleep")
    }
}
A
Start delay
Start sleep
End sleep
End delay
B
Start delay
End delay
Start sleep
End sleep
C
Start sleep
End sleep
Start delay
End delay
D
Start sleep
Start delay
End delay
End sleep
Attempts:
2 left
💡 Hint

Remember that delay is a suspending function that doesn't block the thread, while Thread.sleep blocks the thread.

🧠 Conceptual
intermediate
1:30remaining
Why prefer delay over Thread.sleep in Kotlin coroutines?

Which reason best explains why delay is preferred over Thread.sleep in Kotlin coroutines?

Adelay blocks the thread, making code simpler
BThread.sleep is faster than delay
Cdelay suspends the coroutine without blocking the thread, allowing concurrency
DThread.sleep automatically cancels coroutines
Attempts:
2 left
💡 Hint

Think about how blocking affects other coroutines running on the same thread.

Predict Output
advanced
2:00remaining
Effect of Thread.sleep inside coroutine dispatcher

What will happen when running this Kotlin code?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch(Dispatchers.Default) {
        println("Before sleep")
        Thread.sleep(500)
        println("After sleep")
    }
    launch(Dispatchers.Default) {
        repeat(3) {
            println("Working $it")
            delay(200)
        }
    }
}
A
Before sleep
After sleep
Working 0
Working 1
B
Before sleep
After sleep
Working 0
Working 1
Working 2
C
Before sleep
Working 0
After sleep
Working 1
Working 2
D
Before sleep
Working 0
Working 1
Working 2
After sleep
Attempts:
2 left
💡 Hint

Consider how Thread.sleep blocks the thread and how many threads the Default dispatcher uses.

Predict Output
advanced
2:00remaining
Output of nested delay and Thread.sleep calls

What is the output of this Kotlin coroutine code?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        println("Start outer")
        delay(100)
        launch {
            println("Start inner")
            Thread.sleep(100)
            println("End inner")
        }
        println("End outer")
    }
}
A
Start outer
End outer
Start inner
End inner
B
Start outer
Start inner
End inner
End outer
C
Start outer
Start inner
End outer
End inner
D
Start outer
End outer
End inner
Start inner
Attempts:
2 left
💡 Hint

Remember that launch starts a new coroutine immediately but does not block the parent coroutine.

🧠 Conceptual
expert
2:30remaining
Why does Thread.sleep cause coroutine starvation in single-threaded dispatcher?

In Kotlin coroutines, why can using Thread.sleep inside a coroutine running on Dispatchers.Unconfined or a single-threaded dispatcher cause starvation of other coroutines?

ABecause Thread.sleep suspends only the current coroutine, allowing others to run
BBecause Thread.sleep blocks the thread, preventing other coroutines from running on that thread
CBecause Dispatchers.Unconfined creates multiple threads that compete for CPU
DBecause delay internally uses Thread.sleep, causing blocking
Attempts:
2 left
💡 Hint

Think about what happens when the only thread available is blocked.