winchester044
08/14/2019, 10:55 AMclass Foo {
val dispatcher// An android handler thread as a Coroutine Dispatcher
val job // Parent Job
val coroutineScope = job + dispatcher
fun <T> run(block: () -> T) {
// some prechecks
return runBlocking(coroutinScope.coroutineContext) {
// this runs on dispatcher
}
}
fun shutdown() {
// stop underlying runBlocking
job.cancel()
}
}
Consider the handler thread is busy doing some other stuff and runBlocking
is waiting for it to finish. Meanwhile shutdown()
gets invoked. Is there a way to 'cancel/stop' this runBlocking
call? (Without using thread interrupts, although once lambda has started execution cancellation becomes cooperative).Melih Aksoy
08/14/2019, 1:44 PMMelih Aksoy
08/14/2019, 1:44 PMrunBlocking
will be cancelled with context you provide and end up with JobCancellationException
.winchester044
08/14/2019, 2:19 PMrunBlocking
and current thread is blocked. But runBlocking
is waiting for the dispatcher to become free (its busy doing something else), so, 'actual' execution is yet to start.