note that what you have there has another danger: if you create a new coroutine scope and don't keep any references to it (or something in it) it may be garbage collected and stop running
m
marcinmoskala
07/18/2021, 8:32 AM
I found the answer myself - the first case does not work, because those launches are child of a child of a
SupervisorJob
.
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 AM
This is error resilient:
Copy code
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