https://kotlinlang.org logo
#coroutines
Title
# coroutines
z

zokipirlo

02/06/2019, 12:18 PM
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

streetsofboston

02/06/2019, 12:30 PM
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

zokipirlo

02/06/2019, 2:09 PM
Great, thanks
s

streetsofboston

02/06/2019, 2:44 PM
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

yschimke

02/06/2019, 4:40 PM
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

streetsofboston

02/06/2019, 4:42 PM
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

yschimke

02/06/2019, 4:46 PM
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.
2 Views