Is a stream needed while reading from a socket stream?

I read from a socket input stream like this

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line; while((line = in.readLine()) != null){ // do something Thread.sleep(10); // for example 10ms } 

Now the method of reading the input stream is blocked until the data is available.

In this case, thread cooling is a good idea? After 10 ms, it will still be blocked.

Please do not tell me about non-blocking IO, I know about it.

I'm just wondering if this helps the performance / processor anyway.

+4
source share
2 answers

No. There is no reason to sleep. Why artificially slow down the reading cycle? Let him read the data as fast as he arrives.

+8
source

If you want to allow other threads processor time, you should use:

 Thread.yield(); 

But I don’t think it’s necessary here - let scheduling system threads do their job - that's pretty good.

+3
source

Source: https://habr.com/ru/post/1501691/


All Articles