My program looks like this:
import java.awt.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setSize(new Dimension(200, 200));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
I’m just confused why after JVM’s quitting from main(), my program does not end instantly? I noticed that if I remove the line “jf.setVisible(true);”, it will end.
Is it implemented though techniques like garbage collecting or class destructors? I’m interested that if I want to write something similar, how could I do it.
The reason is that when you call setVisible(true) on the JFrame, behind the scenes a non-daemon thread is started, and the JVM will not exit until all non-daemon threads terminate.
Please have a look here for more on AWT/Swing Threading issues.
It states:
While this is for Java 1.5, I think that it is still valid information.
Also, I believe that the Event Dispatch Thread or EDT is not a daemon thread, and so it is another thread associated with Swing that drives this.
Edit 1
This suggests that the EDT is in fact a non-Daemon thread:
The output from the code is: