https://kotlinlang.org logo
Title
u

uli

04/20/2018, 7:58 PM
Coroutines make using nio simpler. For blocking io they provide little benefit as they will need one thread per io channel anyway
d

Dexter

04/21/2018, 4:24 AM
@uli So I assumed that a coroutine blocked on (a blocking read or write) will suspend allowing other coroutines on the same thread to be dispatched. Let’s say I had one suspend coroutine doing blocking I/O and a bunch of other coroutines on the same performing some sort of compute, are you saying that the compute coroutines will not be dispatched?
u

uli

04/21/2018, 9:22 AM
@Dexter coroutines can not magically turn blocking into suspension. They can be used to turn callbacks into suspension. I.e. with the coroutines.nio package you get calls like aread and awrite. You use aread inside a coroutine just like you would use a blocking read but it will suspend instead of block until the read completes.
d

Dexter

04/21/2018, 9:43 AM
Got it. So just for clarification, if I made a blocking I/O call my whole thread and all associated coroutines on that thread will block on the I/O? That is assuming that my coroutines are not using a context with an underlying thread pool. I think I got this all wrong.
u

uli

04/21/2018, 10:16 AM
Exactly
Same Ist true for long running computations
And instead of a blocking sleep you have delay to suspend for a while
Basically you want no blocking in your coroutines or at least put the blocking on a context with separate threads
d

Dexter

04/21/2018, 8:52 PM
Thanks Uli.