我有 2 个线程 - 一个是接收消息,第二个是接收和发送。
PrintStream os = new PrintStream(sock.getOutputStream());
BufferedReader is = new BufferedReader(new InputStreamReader(sock.getInputStream()));
ServerThread server = new ServerThread(sock, maxid, os,is);
OutputThread out = new OutputThread(sock, maxid, os,is);
然后,当我关闭连接时,我想正确关闭 is 和 os,但是如何在 2 个线程中同时完成呢?我java.net.SocketException
进去ServerThread
public ServerThread(..){
try{
//here I use is and os
} finally {
disconnect();
}
}
public void disconnect() {
try {
System.out.println(addr.getHostName()+ " disconnected");
os.close();
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} finally {
this.interrupt();
}
}