paulblessing
03/27/2019, 2:42 PMJob
.
interface ActiveIndicator {
val isActive: Boolean
fun ensureActive()
}
private class JobActiveIndicator(private val job: Job) : ActiveIndicator {
override val isActive: Boolean get() = job.isActive
override fun ensureActive() {
job.ensureActive()
}
}
suspend fun doSomething() {
val job = coroutineContext[Job]!!
executeCancellableTask(JobActiveIndicator(job))
}
// (Possibly even in Java code somewhere)
fun executeCancellableTask(activeIndicator: ActiveIndicator) {
while (activeIndicator.isActive) {
// Do some work
}
}
elizarov
03/27/2019, 2:44 PMJob
as a “cancellation token” to support cancellation in the otherwise non-coroutines code.