tyjka
08/28/2018, 3:33 AMVsevolod Tolstopyatov [JB]
08/28/2018, 8:06 AMIf yield() is not called on the parent coroutine then child won’t be canceledIt’s not true. Child will be cancelled anyway. But without
yield
child will be cancelled before it has a chance to start, so “Child is cancelled” will not be printed, because it will not be started (as it’s already cancelled)tyjka
08/28/2018, 1:31 PMVsevolod Tolstopyatov [JB]
08/28/2018, 2:09 PMdelay(Long.MAX_VALUE)
call.
Note that child is launched in the same context as parent, which is runBlocking
, so they cannot run concurrently.
With yield:
1) Parent launches child
2) Parent invokes yield
3) Child launches, invokes delay(Long.MAX_VALUE)
and suspends
4) Parent cancels child, so delay(Long.MAX_VALUE)
throws an exception, finally
block executes and “Child is cancelled” is printed
Without yield:
1) Parent launches child
2) Parent cancels child
3) Child immediately cancels, so launch
body is not even executed
4) Parent prints “Parent is not cancelled”