This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nart
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After all the (surviving) non daemon threads created by the application complete (which includes the thread executing main()), the .
JVM will exit.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic