I am reading 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 read method of an input stream blocks until data is available.
In this case is cooling-down the thread a good idea? After 10ms it will be blocking anyway.
Please do not tell me about non-blocking IO, I know about that.
I am just curious whether it helps performance/CPU in anyway.
No. There's no reason to sleep. Why artificially slow down the read loop? Let it read data as fast as it comes in.
If you want to let other threads a cpu time, you should use:
Thread.yield();
But I'm not think it's necessary here- let the system thread scheduling do its job- it's pretty good.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With