为什么要这样?通常,当您显示一个窗口时,您需要控制JVM何时退出. JVM规范中没有任何内容表明当main()方法到达结束时JVM必须退出.
The reason is that AWT encapsulates asynchronous event dispatch machinery to process events AWT or Swing components can fire. The exact behavior of this machinery is implementation-dependent. In particular, it can start non-daemon helper threads for its internal purposes. In fact, these are the threads that prevent the example above from exiting.
这个机器中的一个(三个)限制是这样的:
There is at least one alive non-daemon thread while there is at least one displayable AWT or Swing component within the application (see 07001).
这意味着只要有可显示的AWT / Swing组件,JVM就不会自行退出.
警告:处置所有可显示的组件并不一定意味着非守护程序线程消失了:
It depends on the implementation if and when the non-daemon helper threads are terminated once all components are made undisplayable.