Interview frequency: …
Short answer
In a regular job, the failure of any child cancels the parent and all other children immediately; in a SupervisorJob, the failure is isolated and a child's crash does not affect the other children.
Explanation
This difference is decisive in building UIs and Android libraries (such as viewModelScope, which relies on SupervisorJob). If an image fetch request fails on the screen, you would not want to cancel the request for the user's basic information. With SupervisorJob, each child handles its error independently without tearing down the whole operations tree.
Code example
val supervisorScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
supervisorScope.launch {
throw RuntimeException("Only this child failed")
}
supervisorScope.launch {
// This child continues successfully and is unaffected by its sibling's failure!
delay(1000L)
println("Still working successfully")
}Common mistakes
- Passing SupervisorJob as an argument in launch(SupervisorJob()) and expecting it to isolate children's errors inside it, whereas launch always creates a standard job for its child unless supervisorScope {} is used.