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

Thiyagu

01/17/2020, 8:34 PM
Maybe a vague question for all. What all the benefits I will get if rewrite my Kafka consumer using non blocking I/O and coroutines. Will there be any difference in CPU usage, thread usage compared to my blocking version of the method?
a

Animesh Sahu

01/18/2020, 10:52 AM
Coroutines replaces parallelism with co-operative work, in normal environment of java you usually create a thread and then its dead, or use a thread pool which inturn does almost the same. But for each blocking process instantiating a new thread is a bad idea, because it is an expensive resource, you need os permission and so many checks to create a new thread, instead coroutines manages these things co-operatively using continuation i.e. create a new thread if required or else do the work back in the hood whereever a thread was suspended. For example,
Copy code
a client sent a request to a server and waiting for response, and you want some calculations going on at the same time, then if youre api is blocking you need a new thread for that calculation where as in coroutines you can do the calculation in behalf of the waiting time of the response.
That means instead of blocking we are doing some useful work in the same thread, which is in long term very efficient than that of blocking functions.
👍 1
😄
t

Thiyagu

01/18/2020, 5:30 PM
Hey, Thanks for the briefing. Have you done any jvm profiling kind of experiment to see the actual results?
a

Animesh Sahu

01/19/2020, 10:31 AM
Nope! I didn't did any tests till now