I'm calling plain old blocking java code from a co...
# coroutines
j
I'm calling plain old blocking java code from a coroutine. Any guidance as to when to use
withContext(iodispatcher)
and when to use
runInterruptible(iodispatcher)?
r
If any part of your blocking Java code is throwing InterruptedException (even if it's later caught) you probably want runInterruptible
If not there's unlikely to be much difference between them
z
I don’t think it matters if the java code throws IE. It would get propagated through coroutines either way. You want runInterruptible if your java code supports being interrupted and you want to be able to interrupt it by cancelling the coroutine.
And it uses withContext internally, so all those semantics are still there.
r
Yeah, I was using throws IE as shorthand for handles interruption. Unfortunately it's entirely possible for code to handle interruption and not throw IE, but there's basically no way to know this without going though the entire code
z
Ah ok. “Throws” and “responds to” are very different things though, it’s confusing to conflate them.
r
They can be different things and still be very closely related (I'd classify this as a kind of implied contract at least for JVM standard methods)
Many methods that throw
InterruptedException
, such as
sleep
, are designed to cancel their current operation and return immediately when an interrupt is received.