This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes When will JVM exit? (just one more time pls...) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "When will JVM exit? (just one more time pls...)" Watch "When will JVM exit? (just one more time pls...)" New topic
Author

When will JVM exit? (just one more time pls...)

Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
When does the JVM exit?

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.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Nart
Ranch Hand

Joined: May 28, 2002
Posts: 95
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...
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
After all the (surviving) non daemon threads created by the application complete (which includes the thread executing main()), the .
JVM will exit.
 
Consider Paul's rocket mass heater.
 
subject: When will JVM exit? (just one more time pls...)
 
Similar Threads
JVM
When does the JVM exit
JVM
When does the JVM Exit?
JVM