【发布时间】:2014-04-03 08:25:39
【问题描述】:
我有一个这样的循环:
while (!exit){
read/write on file
}
我的框架上有一个按钮,他的动作改变了“退出”的值,即循环的退出条件。
我的问题,我卡在我的循环中,我无法点击我的按钮,因为我认为程序一直在循环中。
有人告诉我用“轮询”查看线程,但我不明白如何集成到我的循环中?
编辑:
我如何称呼线程:
case "Mode Measure": {
JOptionPane.showMessageDialog(null,"Radar in Measure Mode : ON ");
System.out.println("READ Mode : ON ");
JB_MeasurMode.setVisible(true);
JB_MeasurMode.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
sortie = true;
}
});
SwingUtilities.invokeLater(new Runnable() {
public void run() {
CommPortIdentifier portId = null;
try {
portId = CommPortIdentifier.getPortIdentifier(ChoixPortCom);
serialPort = (SerialPort) portId.open("Main Frame", 5000);
System.out.println("serialPort ouvert");
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
serialPort.setRTS(false);
serialPort.setInputBufferSize(8192);
serialPort.setOutputBufferSize(8192);
serialPort.setSerialPortParams(115400,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_XONXOFF_IN |SerialPort.FLOWCONTROL_XONXOFF_OUT);
catch (NoSuchPortException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (PortInUseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedCommOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Dans le while");
while(sortie == false) {
int i = 0;
int availableBytes = 0;
try {
int read = 0;
availableBytes = inputStream.available();
if (availableBytes > 0) {
String tradV3 = null;
System.out.println("je suis dans le availableBytes > 0 du while -- read = "+read);
read = read + availableBytes;
int raw = inputStream.read(readBuffer, read-availableBytes, availableBytes);
traduction = new String(readBuffer, read-availableBytes, raw);
System.out.println("2=>" + traduction);
tradV3 = tradV3 + traduction; // bytes -> String
}
if (read == 19){
System.out.println("une donnee de 19 char lue -- read = "+read);
System.out.println("Non-Traité :"+Arrays.toString(readBuffer));
int[] tab_int2 = new int[19];
tab_int2 = Fonction.Get_Msg_19(readBuffer);
String Msg_affiche2 = Arrays.toString(tab_int2);
System.out.println("Traité : "+Msg_affiche2);
Measure t = new Measure();
t.GetMeasure(tab_int2);
Tab_Measure[i] = t;
i++;
}
} catch (IOException e) {
System.out.println("Problem avec le try !!!");
}
}
System.out.println("Sortie du while");
}
});
break;
}
【问题讨论】:
-
他是对的。您需要第二个线程来更改
exit的值。 -
另外,while (!exit) 就足够了 (small 'w')
-
好的,但是我把它放在哪里以及如何使用它。你有好的教程吗?
-
@D3fman
While在 Java 中不存在。这是while。来吧,只是谷歌它! -
@Christian:你告诉我这个是因为大写吗? ps:我编辑了。如果我在这里问,也许我在来这里之前搜索过:D
标签: java loops events while-loop polling