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 {
withContext(SupervisorJob())
but works to keep structured concurrencymarcinmoskala
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.fun 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