SupervisorJob lets child tasks fail without stopping their siblings. It helps keep other tasks running even if one fails.
SupervisorJob for independent failure in Kotlin
val supervisor = SupervisorJob() val scope = CoroutineScope(Dispatchers.Default + supervisor)
SupervisorJob is used as a parent job to allow independent failure of child coroutines.
Combine SupervisorJob with a CoroutineScope to launch child coroutines.
val supervisor = SupervisorJob() val scope = CoroutineScope(Dispatchers.Default + supervisor) scope.launch { // child coroutine 1 } scope.launch { // child coroutine 2 }
val supervisor = SupervisorJob() val scope = CoroutineScope(Dispatchers.Default + supervisor) scope.launch { throw Exception("Fail child 1") } scope.launch { delay(1000) println("Child 2 completed") }
This program shows two child coroutines launched under a SupervisorJob. Child 1 throws an exception, but child 2 still completes and prints its message.
import kotlinx.coroutines.* fun main() = runBlocking { val supervisor = SupervisorJob() val scope = CoroutineScope(Dispatchers.Default + supervisor) scope.launch { println("Child 1 starts") throw Exception("Child 1 failed") } scope.launch { delay(500) println("Child 2 completed") } delay(1000) // wait for children }
Without SupervisorJob, if one child fails, all siblings get cancelled.
SupervisorJob helps build fault-tolerant concurrent code.
Always handle exceptions in child coroutines to avoid silent failures.
SupervisorJob allows child coroutines to fail independently.
It prevents one failure from cancelling all sibling coroutines.
Use it to build more resilient concurrent programs.
Practice
SupervisorJob in Kotlin coroutines?Solution
Step 1: Understand SupervisorJob behavior
SupervisorJoblets child coroutines run independently so one failure doesn't affect others.Step 2: Compare with regular Job
RegularJobcancels all children if one fails, butSupervisorJobprevents this.Final Answer:
To allow child coroutines to fail independently without cancelling siblings -> Option BQuick Check:
SupervisorJob = independent failure [OK]
- Thinking SupervisorJob cancels all children on failure
- Confusing SupervisorJob with CoroutineScope
- Assuming SupervisorJob restarts failed coroutines
CoroutineScope with a SupervisorJob?Solution
Step 1: Identify correct SupervisorJob usage
Creating a scope withSupervisorJob()instance plus a dispatcher is correct syntax.Step 2: Check options for syntax correctness
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default) usesSupervisorJob()constructor and adds dispatcher, which is valid.Final Answer:
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default) -> Option AQuick Check:
SupervisorJob() + Dispatcher = correct scope [OK]
- Using SupervisorJob without parentheses
- Not adding a dispatcher to CoroutineScope
- Using Job() instead of SupervisorJob()
SupervisorJob:
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
scope.launch {
launch { throw Exception("Child 1 failed") }
launch { delay(100); println("Child 2 completed") }
}
What will be printed when this code runs?Solution
Step 1: Analyze SupervisorJob effect on children
WithSupervisorJob, failure in one child does not cancel siblings.Step 2: Trace coroutine execution
Child 1 throws exception immediately, but Child 2 delays and then prints "Child 2 completed".Final Answer:
Child 2 completed -> Option AQuick Check:
SupervisorJob allows sibling to finish [OK]
- Assuming all children cancel on one failure
- Expecting no output due to exception
- Confusing exception print with println output
SupervisorJob:
val scope = CoroutineScope(SupervisorJob)
scope.launch {
launch { throw Exception("Error") }
launch { println("Running") }
}Solution
Step 1: Check SupervisorJob usage
SupervisorJobis a function and must be called with parenthesesSupervisorJob().Step 2: Validate CoroutineScope creation
UsingSupervisorJobwithout parentheses is a syntax error; it must be an instance.Final Answer:
SupervisorJob is used without parentheses, causing a syntax error -> Option DQuick Check:
SupervisorJob() requires parentheses [OK]
- Omitting parentheses on SupervisorJob
- Thinking nested launch is invalid
- Assuming dispatcher is mandatory (optional)
SupervisorJob and handles exceptions independently?Solution
Step 1: Choose SupervisorJob for independent failure
UsingSupervisorJob()allows children to fail independently without cancelling siblings.Step 2: Handle exceptions in each child
Wrapping each child launch in try-catch blocks ensures exceptions are caught and handled independently within each coroutine.Final Answer:
Use CoroutineScope(SupervisorJob()) and wrap each child launch in try-catch blocks -> Option CQuick Check:
SupervisorJob + try-catch = safe independent children [OK]
- Ignoring exception handling in children
- Using Job() which cancels siblings on failure
- Using GlobalScope without SupervisorJob
Ready to prove it?
Pass the test and this pattern moves unknown
