我有一个创建并运行线程的类,它创建了一个GUI。 我希望初始类在GUI关闭之前保持挂起(例如,OK按钮)
我尝试thread.join();
但是由于GUI是在事件调度线程上创建的,所以这似乎不起作用,并且当GUI弹出时,类继续。
private void CreateAndRunThread(){
GUIMaker GM= new GUIMaker(data);
GM.run();
try {
TFM.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MessageDialog.showDialog("GM Done");
}
线程的GUI创建:
@Override
public void run() {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
CreateAndShowGUI(frame); //adds frame, packs and sets visible
}
});
}
答案 0 :(得分:1)
使用CountDownLatch:
CountDownLatch latch = new CountDownLatch(1);
在inital类中调用以下内容来阻止:
latch.await();
关闭GUI时调用以下命令:
latch.countDown();
此外,您似乎没有正确启动线程。您需要调用GM.start()方法而不是GM.run()。