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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.