Challenge - 5 Problems
Job Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this coroutine job cancellation code?
Consider the following Kotlin code using coroutines. What will be printed when this code runs?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { val job = launch { repeat(3) { i -> println("Working on $i ...") delay(100L) } } delay(250L) job.cancel() job.join() println("Done") }
Attempts:
2 left
💡 Hint
Think about when the job is cancelled and how many iterations complete before cancellation.
✗ Incorrect
The job starts and prints 'Working on 0 ...' (~0ms), 'Working on 1 ...' (~100ms), and 'Working on 2 ...' (~200ms), each followed by a 100ms delay. After 250ms, the job is cancelled during the third delay, so no further iterations run. Then 'Done' is printed.
🧠 Conceptual
intermediate1:30remaining
Which state does a Job enter after calling cancel() but before completion?
In Kotlin coroutines, after you call cancel() on a Job but before it finishes, what is the Job's state?
Attempts:
2 left
💡 Hint
Think about the transition phase between active and fully cancelled.
✗ Incorrect
After cancel() is called, the Job enters the 'Completing' state while it finishes its cancellation process. It is not yet fully 'Cancelled' until completion.
🔧 Debug
advanced2:30remaining
Why does this coroutine not cancel as expected?
Examine the code below. The job.cancel() is called, but the coroutine keeps running. Why?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { val job = launch { for (i in 1..5) { Thread.sleep(100L) println("Working $i") } } delay(250L) job.cancel() job.join() println("Finished") }
Attempts:
2 left
💡 Hint
Consider how blocking calls affect coroutine cancellation.
✗ Incorrect
Thread.sleep blocks the thread and does not check for cancellation, so the coroutine cannot be cancelled during that sleep. Using delay instead allows cancellation.
📝 Syntax
advanced1:30remaining
Which option correctly cancels a Job and waits for its completion?
Select the Kotlin code snippet that cancels a Job and waits until it finishes properly.
Attempts:
2 left
💡 Hint
Look for the built-in function that combines cancel and join.
✗ Incorrect
cancelAndJoin() cancels the job and suspends until it completes. Calling cancel() then join() separately works but is less concise. Option C is the idiomatic way.
🚀 Application
expert2:30remaining
How many times will 'Task running' print before cancellation?
Given the code below, how many times will 'Task running' be printed before the job is cancelled?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { val job = launch { repeat(10) { i -> if (!isActive) return@launch println("Task running $i") delay(50L) } } delay(230L) job.cancelAndJoin() println("Job cancelled") }
Attempts:
2 left
💡 Hint
Calculate how many 50ms delays fit into 230ms before cancellation.
✗ Incorrect
Each iteration delays 50ms. After 230ms, about 4 full iterations complete and the 5th starts. The 5th prints before cancellation, so total prints are 5.