我有一个布尔变量来控制服务器的执行(启动/停止):
私人布尔生态=真;
这是我的课:
sw=new SwingWorker<String,Void> (){
protected String doInBackground() throws Exception {
try {
server = new ServerSocket(Integer.parseInt(port.getText()));
String str1="waiting for connexion..";
String str2="Connexion ok";
log.append(str1+"\n");
PrintWriter out=null;
BufferedReader in=null;
Socket socClient=null;
while(ecoute){
socClient = server.accept();
log.append(str2+"\n");
in = new BufferedReader(
new InputStreamReader(socClient.getInputStream())
);
out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(socClient.getOutputStream())),
true);
String str = in.readLine();
log.append(str+"\n");
}
in.close();
out.close();
socClient.close();
return "***End Reception***";
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
return "***Error Reception***";
}
}
protected void done(){
String m="";
try {
m=get();
} catch (InterruptedException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
log.append(m+"\n");
}
};
sw.execute();
}
当我单击按钮将变量传递给 false 以使我的线程退出无限循环时,没有任何反应:
private void arretReceptionActionPerformed(java.awt.event.ActionEvent evt) {
ecoute=false;
}
我也没有什么新东西取而代之ecoute=false;
... sw.calcel(true);
有什么建议吗?