https://kotlinlang.org logo
#announcements
Title
# announcements
l

littlelightcz

10/27/2017, 4:41 PM
Hi everybody, is it possible to execute a blocking IO operation (such as HTTP GET request) in a coroutine and make it suspend instead of blocking the thread?
k

karelpeeters

10/27/2017, 4:52 PM
#coroutines or google 🙂
l

littlelightcz

10/27/2017, 4:54 PM
the point is I tried it and it seems the coroutines are still blocking but not suspending ... so there is still only 1 coroutine per 1 thread - the only benefit is that a coroutine starts faster, but that's it
but thanks for the coroutines channel, I will give it a try there 🙂
k

karelpeeters

10/27/2017, 4:56 PM
Well to call blocking code you'll always need to "dedicate" a thread to that call.
l

littlelightcz

10/27/2017, 4:57 PM
hmh, so that means that basically if (probably) every IO operation in Java is blocking while waiting for an IO response, the coroutines can't do anything about that?
k

karelpeeters

10/27/2017, 4:58 PM
Yes. You can use a non-blocking library like
java.nio
I think.
l

littlelightcz

10/27/2017, 5:00 PM
hmh so maybe that could be the solution, I will check it, but otherwise it would have been quite pity 🙂
b

bj0

10/27/2017, 5:00 PM
coroutines don't magically turn a blocking call into a non-blocking call, the blocking call will always block the thread it is on. you have to use a non-blocking version/api (like
java.nio
OR you can push the blocking call into a background thread so it doesn't block any other threads. The coroutine library makes this second option easy, since there are some blocking apis that can't be easily replaced
k

karelpeeters

10/27/2017, 5:00 PM
Well of course it is possible to do IO on coroutines, that's pretty much the point.
l

littlelightcz

10/27/2017, 5:05 PM
Thanks guys, I think that now I understand what is going on 🙂. I am going to look at the NIO Java lib just for the sake of exploration 🙂
2 Views