marcinmoskala
07/17/2021, 8:25 AMSupervisorJob
is designed in this way, that it needs to be a part of scope, and does not work as a simple context passed as an argument?
fun main(): Unit = runBlocking(SupervisorJob()) {
launch {
delay(100)
throw Error()
}
launch {
delay(200)
print("Done")
}
}
// Just Error
fun main(): Unit = runBlocking {
with(CoroutineScope(coroutineContext + SupervisorJob())) {
launch {
delay(100)
throw Error()
}
val job = launch {
delay(200)
print("Done")
}
job.join()
}
}
// Error...
// Done
ephemient
07/17/2021, 5:51 PMsupervisorScope {
ephemient
07/17/2021, 5:54 PMwithContext(SupervisorJob())
but works to keep structured concurrencyephemient
07/17/2021, 5:57 PMmarcinmoskala
07/18/2021, 8:32 AMSupervisorJob
. runBlocking
makes a child job, and launch
also makes a child based on the runBlocking
job. SupervisorJob
gives a special behaviour only for its children, not children of its children.marcinmoskala
07/18/2021, 8:34 AMfun main(): Unit = runBlocking {
val s = SupervisorJob()
launch(s) {
delay(100)
throw Error()
}
launch(s) {
delay(200)
print("Done")
}
delay(300)
s.complete()
s.join()
}
// Error
// Done