If I `launch` a coroutine where I call one long bl...
# coroutines
z
If I
launch
a coroutine where I call one long blocking method (some library in native code) on
<http://Dispatcher.IO|Dispatcher.IO>
, is it possible that this coroutine executes on a thread on which some other coroutine is running and will block execution of that other coroutine?
s
Possible but unlikely, because the pool of threads of
IO
is large.
And note that a thread can only execute one thing at a time. If one coroutine is suspended, another is blocking, the one coroutine can continue later in an other thread from the
IO
thread pool, while the other is still blocked.
💯 1
z
Great, thanks
s
And of course, even when using
IO
, when you do something like this, all bets are off:
Copy code
for (i in 0..1000) { launch(<http://Dispatchers.IO|Dispatchers.IO>) { Thread.sleep(500) } }
🙂
y
I don’t know whether this is correct or not but my mental model of IO is an almost infinite pool of threads that are expected to be mostly blocked on synchronous blocking io operations.
s
It is a limited pool, but the limit is high. I think about 64 threads (or the number of CPUs, which ever one is higher).
y
When the docs say it shares threads with Default. Is that a one way subset? E.g. default won’t use an IO thread unless work is already being done there. To avoid context switch.