Filedialog 正在杀死我的线程
我正在使用 Java 开发一个 socket 程序。 我在后台运行带有套接字服务器的GUI。 套接字服务器正在运行一个线程,每 10 毫秒检查一次套接字消息。 它们都运行良好,但是当我尝试在 gui 中打开文件对话框时,gui 崩溃,但服务器继续运行。 我认为我以错误的方式运行服务器(或服务器线程)。 如果我跳过套接字,文件对话框可以正常工作。
可能是什么问题,是否我以错误的方式运行线程?
(这是在一堂课中)
public ServerController(){
ServSocket st = new ServSocket();
Thread thread1=new Thread(st);
thread1.start();
}
(这是我的线程)
public void run(){
while (true) {
try {
Thread.sleep(10);
}
catch (InterruptedException e) {}
switch (Status) {
case CONNECTED:
try {
socket = new Socket(hostIP, port);
System.out.println("Connected on: " + hostIP + port);
out = new PrintWriter(socket.getOutputStream(), true);
changeStatus(STARTSENDING, true);
}
catch (IOException e) {
System.out.println("disconnected");
}
break;
(这是我的主线程)
static ServerController scon;
static Controller cn;
public static void main(String[] args) {
scon = new ServerController();
cn = new Controller();
cn.gui();
}
Im working on a socket program in Java.
Im running a GUI with a socket server in the background.
The socket server is running a thread that checks for socket messages every 10ms.
Both of them runs fine together but as soon as I try to open my File dialog in the gui, the gui crashes, but the server keeps on running.
Im thinking that I run the server (or the server thread) in a wrong way.
The file dialog works fine if I skip the socket.
What could be the problem, could it be that Im running the thread in a wrong way?
(this in one class)
public ServerController(){
ServSocket st = new ServSocket();
Thread thread1=new Thread(st);
thread1.start();
}
(this is my thread)
public void run(){
while (true) {
try {
Thread.sleep(10);
}
catch (InterruptedException e) {}
switch (Status) {
case CONNECTED:
try {
socket = new Socket(hostIP, port);
System.out.println("Connected on: " + hostIP + port);
out = new PrintWriter(socket.getOutputStream(), true);
changeStatus(STARTSENDING, true);
}
catch (IOException e) {
System.out.println("disconnected");
}
break;
(and this is my main)
static ServerController scon;
static Controller cn;
public static void main(String[] args) {
scon = new ServerController();
cn = new Controller();
cn.gui();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是猜测,但我认为这与 EDT 有关。
您是否尝试从 EDT 之外启动该对话框? http://en.wikipedia.org/wiki/Event_dispatching_thread
如果您认为可能,请尝试使用 SwingUtilities 静态方法(特别是 isEventDispatchThread 和 invokeLater)来磨练并纠正问题:
http://java.sun.com/javase/6/docs/api/javax/swing/SwingUtilities.html#isEventDispatchThread()
http://java.sun.com/javase/6/docs /api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)
hth
Just guessing here, but I think it's relating to the EDT.
Are you trying to launch the dialog from outside the EDT? http://en.wikipedia.org/wiki/Event_dispatching_thread
If you think you might be, try using SwingUtilities static methods (specifically isEventDispatchThread and invokeLater) to hone in and rectify the issue:
http://java.sun.com/javase/6/docs/api/javax/swing/SwingUtilities.html#isEventDispatchThread()
http://java.sun.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)
hth
现在问题已经解决了。
似乎问题是我有一个扫描仪在线程中每 10 毫秒等待输入(
string = sc.next();
),在几次输入后我的 GUI 显示了。我删除了扫描仪,现在我有了一个可以工作的应用程序。
The problem is now solved.
Seems that the problem was that i had a scanner that was waiting for input(
string = sc.next();
) every 10ms in the thread, and after a few input my GUI showed.I removed the Scanner and i now have a working application.