Lukas Lechner
08/10/2020, 8:12 AMfun main() = runBlocking {
val job = launch {
repeat(5) { index ->
println("operation number $index")
ensureActive()
Thread.sleep(100)
}
}
delay(150)
job.cancel()
}
octylFractal
08/10/2020, 8:15 AMrunBlocking
is a single thread, so job.cancel()
never runs until it's already finishedTijl
08/10/2020, 8:16 AMoctylFractal
08/10/2020, 8:17 AMensureActive()
with yield()
, or delay
instead of Thread.sleep
, or use <http://Dispatcher.IO|Dispatcher.IO>
-- any of these will solve the issue, and may be appropriate depending on what you're actually doingLukas Lechner
08/10/2020, 8:19 AMensureActive
is a not a supend
function, whereas yield()
is one 🤔