have you ever wondered why its necessary to call system.exit() to force your java ui application to exit? have a look at the code in listing 1.
after the setvisible() method has been called at line 15, the main() method completes execution. why doesnt the program end at that point?
the answer to this question is rooted in a subtlety of javas thread creation mechanism.
java makes a distinction between a user thread and another type of thread known as a daemon thread. the difference between these two types of threads is straightforward. if the java runtime determines that the only threads running in an application are daemon threads (i.e., there are no user threads), the java runtime promptly closes down the application. on the other hand, if at least one user thread is alive, the java runtime wont terminate your application. in all other respect(s) the java runtime treats daemon threads and user threads in exactly the same manner.
when your main() method initially receives control from the java runtime, it executes in the context of a user thread. as long as the main-method thread or any other user thread remains alive, your application will continue to execute.
to see just how a thread becomes a daemon thread, read on.
going to the devil
there are two ways a thread can become a daemon thread (or a user thread, for that matter) without putting your soul at ... 下一页