0
0
Kotlinprogramming~10 mins

SupervisorJob for independent failure in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - SupervisorJob for independent failure
Start SupervisorJob
Launch Child 1
Launch Child 2
Child 1 Fails?
YesChild 2 continues
Child 2 completes
SupervisorJob completes
Parent Coroutine completes
SupervisorJob lets child coroutines fail independently without cancelling siblings or the parent.
Execution Sample
Kotlin
val supervisor = SupervisorJob()
val scope = CoroutineScope(supervisor)
scope.launch { throw Exception("Fail 1") }
scope.launch { delay(100); println("Child 2 done") }
Two child coroutines run under SupervisorJob; one fails, the other completes normally.
Execution Table
StepActionChild 1 StateChild 2 StateSupervisorJob StateOutput
1Start SupervisorJob and CoroutineScopeNot startedNot startedActive
2Launch Child 1RunningNot startedActive
3Launch Child 2RunningRunningActive
4Child 1 throws ExceptionFailedRunningActiveException("Fail 1") thrown
5Child 2 continues after delayFailedRunningActive
6Child 2 prints messageFailedCompletedActiveChild 2 done
7SupervisorJob completes after children finishFailedCompletedCompleted
8Parent CoroutineScope completesFailedCompletedCompleted
💡 Child 1 failure does not cancel Child 2 or SupervisorJob; all complete independently.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 6Final
Child 1 StateNot startedRunningRunningFailedFailedFailed
Child 2 StateNot startedNot startedRunningRunningCompletedCompleted
SupervisorJob StateActiveActiveActiveActiveActiveCompleted
Key Moments - 2 Insights
Why does Child 2 keep running even though Child 1 failed?
Because SupervisorJob isolates failures; as shown in step 4 and 5 of execution_table, Child 1 fails but SupervisorJob stays active and Child 2 continues.
Does the failure of Child 1 cancel the SupervisorJob?
No, SupervisorJob remains active until all children complete, as seen in step 7 where SupervisorJob completes only after Child 2 finishes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of Child 1 at step 5?
ARunning
BFailed
CCompleted
DNot started
💡 Hint
Check the 'Child 1 State' column at step 5 in execution_table.
At which step does Child 2 print its message?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look for the 'Output' column mentioning 'Child 2 done' in execution_table.
If SupervisorJob was replaced with a regular Job, what would happen after Child 1 fails?
ASupervisorJob cancels Child 2
BParent CoroutineScope completes immediately
CChild 2 continues running
DNothing changes
💡 Hint
Recall SupervisorJob isolates failures; a regular Job cancels siblings on failure.
Concept Snapshot
SupervisorJob lets child coroutines fail independently.
Failure in one child does NOT cancel siblings or the parent.
Use CoroutineScope(SupervisorJob()) to isolate failures.
Child coroutines run concurrently; failures are isolated.
SupervisorJob completes only after all children finish.
Full Transcript
This visual trace shows how SupervisorJob manages child coroutine failures independently. We start a SupervisorJob and launch two child coroutines. Child 1 throws an exception and fails, but Child 2 continues running and completes successfully. The SupervisorJob remains active until all children finish, then completes. This means failure in one child does not cancel others or the parent scope. The variable tracker confirms the states of children and SupervisorJob at each step. Key moments clarify why Child 2 is unaffected by Child 1's failure. The quiz tests understanding of states and SupervisorJob behavior. This helps beginners see how SupervisorJob isolates failures in Kotlin coroutines.