我需要连续监听串行输入,但NetBeans警告“在循环中调用Thread.sleep会导致性能问题。” 我理解为什么这会导致性能问题,但Java中最好的选择是什么?
public class SerialIO extends javax.swing.JFrame {
...
class RcvTask extends Thread {
public void run() {
try {
SerInputStream sis = new SerInputStream(serialPort);
InputStreamReader isr = new InputStreamReader(sis);
BufferedReader reader = new BufferedReader(isr);
while (true) {
String line = reader.readLine();
parse(line);
sleep(RX_SLEEP);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
...
答案 0 :(得分:21)