1. After the main method returns. 2. After all the non daemon threads created by the application complete. 3. After all the daemon threads created by the application complete. 4. When a thread executes System.exit(); 5. When an uncaught exception is thrown in a non daemon thread. 6. When an uncaught exception is thrown in a daemon thread.
About options 5 & 6 being valid answers, I've looked at the posts, and there seem to be three answers to this: - JVM exits when an exception is not caught - JVM exits when an exception thrown from main is not caught, otherwise it doesn't - JVM doesn't exit when an exception is not caught, only the thread throwing the exception is terminated. I think the last option is correct. I hope someone clears this up, though.
Joined: Dec 20, 2001
When in doubt, write some code! Here's a snippet I just whipped up:
If you executed this, you'll see that the new thread that I spawned throws a RuntimeException instantly and dies. However, the main thread remains alive until it can print out the final statement. Therefore, if an exception goes uncaught, only the thread from which that exception was thrown is terminated abruptly. I hope that helps, Corey
hi... this is from Java How To Program 3rd Edition by Deitel & Deitel... "... A Program can include a mixture of daemon and non-daemon threads. When only daemon threads remain in a program, the program exits." hope that helps you...
Joined: Mar 22, 2002
After all the (surviving) non daemon threads created by the application complete (which includes the thread executing main()), the . JVM will exit.