streetsofboston
07/02/2019, 3:04 PMSupervisorJob
.
This code
CoroutineScope(SupervisorJob()).launch {
launch {
println("Launched Child 1")
delay(500)
println("Child 1 crashed")
throw SillyException()
}
launch {
println("Launched Child 2")
delay(1000)
println("Child 2 is still running")
delay(500)
}
delay(1000)
println("Parent is still running")
}
Thread.sleep(2000)
prints out this:
Launched Child 1
Launched Child 2
Child 1 crashed
Exception in thread "DefaultDispatcher-worker-2 @coroutine#3" launch_exceptions.SillyException: Silly
at launch_exceptions.FileKt$main$1$1.invokeSuspend(File.kt:20)
...
I expected only “Child 1” to crash/stop, due to the parent being a Supervisor-Job .
However, I expected “Child 2" launch and the parent to continue running…
especially since MainScope()
is defined in a similar way…Evgeniy Zaharov
07/02/2019, 3:11 PMlaunch
is start as child for outer launch
, which is not a SupervisorJob
. Only childs of CoroutineScope(SupervisorJob())
fails independently from each otherstreetsofboston
07/02/2019, 3:19 PMval scope = CoroutineScope(SupervisorJob())
scope.launch {
println("Launched Child 1")
delay(500)
println("Child 1 crashed")
throw SillyException()
}
scope.launch {
println("Launched Child 2")
delay(1000)
println("Child 2 is still running")
delay(500)
}
louiscad
07/02/2019, 3:46 PMsupervisorScope { … }
also, and launch all the coroutines here.streetsofboston
07/02/2019, 3:48 PMsupervisorScope
is a suspend
fun and can’t be called from a regular non-suspending function.Dico
07/02/2019, 7:06 PM