0
0
Kotlinprogramming~10 mins

Delay vs Thread.sleep in Kotlin - Interactive Practice

Choose your learning style9 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.