So I just switch to `Dispatcher.Default` from `<ht...
# coroutines
h
So I just switch to
Dispatcher.Default
from
<http://Dispatcher.IO|Dispatcher.IO>
to handle a
ProcessBuilder
and got this recommendation from the intellij idea:
Possibly blocking call in non-blocking context could lead to thread starvation
. I used
waitFor
on the
ProcessBuilder
so it's probably why the idea suggested to switch back to
<http://Dispatcher.IO|Dispatcher.IO>
, does the idea suggest this for every operation that gonna block the thread its run on without knowing whether the operation depend on CPU or IO more?
r
ProcessBuilder
creates a new external process that doesn't use your current process's resources. Your current thread should use
IO
dispatcher to wait for the external process because the current thread is not computing anything; the current thread is only waiting. It is very much the same situation as if your external process were replaced by a remote server that you access via the internet. Your current thread is waiting for input/output from something external, so it should use
IO
dispatcher. It doesn't matter if the external process/server is doing its own CPU work.
👍 1
👍🏼 1
g
> does the idea suggest this for every operation that gonna block the thread its run on without knowing whether the operation depend on CPU or IO more It's correct, this check is not very sophisticated, it mostly relies on what kind of exceptions specific function throws And as Ryan mentioned, you just need IO here
You also should use runInterruptible, so it will work better with coroutines cancellation, because waitFor is a code with thread interruption support https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-interruptible.html
h
thanks @rkechols
@gildor I see, what will happen to the calling coroutine if
waitFor
return an
InterruptedException
in the case that I don't use
runInterruptible
?
g
If coroutine where you run would be cancellable, waitFor continue blocking coroutine and thread
h
How is that the cancelled coroutine still be blocked by
waitFor
and not get GC-ed?
g
because thread is blocked, it does work, cannot be reused for anythign else with cancellign task
👍 1