我的 Java 程序有问题。我有这个代码:
Host.java:
public class Host {
protected static void start(JFrame window) {
ServerSocket server = null;
try {
server = new ServerSocket();
SocketAddress addr = new InetSocketAddress(hostname, port);
server.bind(addr);
Socket socket = server.accept();
window.setVisible(false);
Thread thread = new Thread(new Incomming(socket.getInputStream()));
thread.start();
thread.join();
socket.close();
} catch (UnknownHostException e) {
[...]
}
}
Incomming.java:
public class Incomming implements Runnable {
private DataInputStream is;
public Incomming(InputStream is) {
MyFrame frame = new MyFrame();
frame.setVisible(true);
frame.pack();
this.is = new DataInputStream(is);
}
public void run() {
try {
while(!Thread.currentThread().isInterrupted()) {
int n = is.readInt();
if(n == -1) {
break;
}
byte[] b = new byte[n];
is.readFully(b);
[...] // working with bytes
}
System.out.println("Stream closed.");
} catch(IOException e) {
[...]
}
}
}
Client.java与Host.java非常相似,它也使用Incomming.java作为socket.getInputStream()。
所以问题是:客户端连接到主机,但是当它应该在服务器端和客户端上显示 MyFrame 窗口时,它没有完全加载。旧 JFrame 窗口(两侧)的关闭按钮没有任何作用。
我尝试使用 thread.join()
删除该行,然后 MyFrame 窗口完全加载并关闭按钮工作,但它向我抛出异常 socket close
消息,因此客户端不再连接到主机。
我该如何解决这个问题? 感谢您的回复。
最佳答案
- Thread#join 将阻塞,直到线程死亡。这会阻止您关闭窗口,因为您正在阻塞事件调度线程。请参阅Concurrency in Swing了解更多详情。
- 您
接受
传入的套接字,启动一个新的线程
来处理该套接字,然后立即关闭该套接字(很可能在线程有机会启动之前)从中阅读)。相反,一旦完成对流的处理,就关闭线程内的套接字
已更新
Swing 是一个单线程框架。这意味着与 UI 的所有交互(创建、修改)必须在事件调度线程的上下文中执行。任何阻塞该线程的操作都会阻止 EDT 处理事件,包括重绘、鼠标和键盘事件。
您应该传递套接字,而不是将套接字的输入流传递给线程。这会将套接字管理的责任传递给该线程,从而释放当前线程。
然后在您的 Incomming
类中,您应该获取对套接字输入流的引用,执行您需要的任何操作,最后,在以下情况下关闭输入流和套接字你已经完成了。
protected static void start(JFrame window) {
ServerSocket server = null;
try {
server = new ServerSocket();
SocketAddress addr = new InetSocketAddress(hostname, port);
server.bind(addr);
Socket socket = server.accept();
window.setVisible(false);
// Pass the socket to the thread to allow it to perform the work
Thread thread = new Thread(new Incomming(socket));
thread.start();
} catch (IOException ex) {
//...//
}
}
public class Incomming implements Runnable {
private final Socket socket;
public Incomming(Socket socket) {
//?? What's this for, this is VERY wrong
// UI Interaction should ONLY occur within the context of the EDT
MyFrame frame = new MyFrame();
frame.setVisible(true);
frame.pack();
this.socket = socket;
}
public void run() {
if (socket != null) {
DataInputStream is = null;
try {
is = new DataInputStream(socket.getInputStream());
while (!Thread.currentThread().isInterrupted()) {
int n = is.readInt();
if (n == -1) {
break;
}
byte[] b = new byte[n];
is.readFully(b);
//...//
}
System.out.println("Stream closed.");
} catch (IOException e) {
} finally {
// Finally clean up...
try {
is.close();
} catch (Exception e) {
}
try {
socket.close();
} catch (Exception e) {
}
}
}
}
}
您必须阅读Concurrency in Swing
如果您打算在处理套接字时更新 UI,您很可能希望使用 SwingWorker
而不是 Thread
。这提供了额外的功能,可以更轻松地将更新同步回事件调度线程
关于java - 使用线程时客户端/服务器 Swing 程序卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17602685/