I have snippet of RXTX
serial communication sample:
public static class SerialReader implements Runnable {
InputStream in;
public SerialReader(InputStream in) {
this.in = in;
}
public void run(){
byte[] buffer = new byte[1024];
int len = -1;
try {
while ((len = this.in.read(buffer)) > -1){
System.out.print(new String(buffer,0,len));
}
}
catch(IOException e) {
e.printStackTrace();
}
}
}
I'm wondering regarding while
cycle. According to my understanding such non ending while cycles should be overkill for system and should be avoided. But when I looked ad task manager I couldn't find significant load.
Then I changed while
cycle as below and got system overload.
while (true) {
System.out.print(new String(buffer,0,0));
}
Why firs approach not generates high load? How I can make second cycle to make not so hungry for CPU? Is it good practice to use such algorithms at all?