[英]Why the new thread also exits after closing of Swing GUI in Java
这是我程序的代码。 问题是,每当我关闭主GUI时,我的新线程也会退出。 从定义上讲,这不应该是因为它不是守护程序线程。 我正在日食中工作,任何帮助将不胜感激。
public class HomeScreen extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTable table;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
HomeScreen frame = new HomeScreen();
frame.setVisible(true);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while(true){
System.out.println("Hello");//This should print "Hello" forever but this thread exits after i close main GUI
}
}
});
t.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
谢谢你们。 我得到的技巧只是添加了一条语句来无限运行线程
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.