private fun startCoroutines() {
GlobalScope.launch(Dispatchers.Main) {
withTimeout(1000L) {
println("This is executed before the first delay")
stallForTime()
println("This is executed after the first delay")
}
printAfterSomething("the timeout")
}
println("This is executed immediately")
}
suspend fun stallForTime() {
withContext(Dispatchers.Default) {
delay(2000L)
}
}
after the child job inside
withTimeout
does not finish in time (because stallForTime made it take too long (2 seconds))
why does the
printAfterSomething
not get called
oday
09/16/2022, 5:29 PM
or otherwise, why does the parent seemingly also get canceled? the child didnt crash or anything, nor did I call cancel, why am I not seeing the
printAfterSomething
print statement?
oday
09/16/2022, 5:30 PM
ok sorry got it, cause withTimeout throws an exception, and if exception is thrown inside the child, parent cancels as well
j
jw
09/16/2022, 5:44 PM
Yes, but note that there's no parent/child here. There's just one coroutine that's moving around between dispatchers and through functions.
o
oday
09/16/2022, 6:02 PM
ok then I dont know what makes a job a child of another, which caused me to face another problem with another example here if you have the time to look
https://klassbook.commonsware.com/lessons/Coroutine%20Basics/cancel-sans-cooperation.html
so line 24 here, this cancels that job right? Ok
this example is showing how that busyWait and print dont honor cancellation request
oday
09/16/2022, 6:03 PM
yea i dont even know what the question is, I dont understand what is going on there and how to make the job wait for cancellation and not print the subsequent lines….i tried to call join on that job or cancelAndJoin but I am not even sure what join does….
oday
09/16/2022, 6:07 PM
ignore the above, sorry about that, it’s of course cancelAndJoin so it could wait until cancelation is done and NOT print them
oday
09/17/2022, 1:04 PM
@jw you still hit IRC somewhere? or all Slack now?