benny.huo
04/16/2019, 11:31 PMjoin
function:louiscad
04/16/2019, 11:59 PMjoin()
doesn't throw if the job is cancelled but didn't crash (i.e. threw a CancellationException
)benny.huo
04/17/2019, 12:05 AMsuspend fun main() {
log(1)
val job = GlobalScope.launch(Dispatchers.Unconfined) {
log(2)
val job2 = launch(Dispatchers.Default) {
log(3)
val x = 1 / 0
log(4)
}
// if falls in the fast-path, a CancellationException will be thrown.
job2.join()
log(5)
}
log(6)
job.join()
log(7)
}
louiscad
04/17/2019, 12:12 AMlouiscad
04/17/2019, 12:12 AMcoroutineScope { … }
benny.huo
04/17/2019, 12:20 AMGlobeScope
? I changed to coroutineScope{...}
and it seems to work normally.louiscad
04/17/2019, 12:22 AMbenny.huo
04/17/2019, 12:25 AMGlobalScope
?streetsofboston
04/17/2019, 12:51 AMcoroutineScope
inherits its scope (the receiver of the lambda) from the calling/outer scope.
Using GlobalScope here will cause you to switch scope from the calling one to the GlobalScope. Cancelation and error-handling may get wonky...
Use GlobalScope if a coroutine runs as part of an object with a global lifecycle, eg a singleton global object.benny.huo
04/17/2019, 1:39 AMVsevolod Tolstopyatov [JB]
04/17/2019, 11:45 AMbenny.huo
04/17/2019, 11:18 PMcoroutineScope
, CancallationException still happens when the child job’s join()
is called. Just like GlobalScope does.benny.huo
04/17/2019, 11:21 PMjoinSuspend
.benny.huo
04/17/2019, 11:27 PMbenny.huo
04/17/2019, 11:29 PMbenny.huo
04/18/2019, 3:23 AMjoin()
call on child job canceled. While join on the running child job, when exception throws, the suspended join returns with a Unit and the parent job will be canceled later on. That makes sense although confusing.louiscad
04/18/2019, 4:52 AMCancellationException
is not considered as a crash, but other throwables are.