permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes When does the JVM exit? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "When does the JVM exit? " Watch "When does the JVM exit? " New topic
Author

When does the JVM exit?

rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116
When does the JVM exit?

1. After the main method returns.
2. After all the non demons threads created by the application complete.
3. After all the demon threads created by the application complete
4. When a thread executes System.exit();
5. When an uncaught exception is thrown in a non demon thread.
6. When an uncaught exception is thrown in a demon thread.
My Answer: 24
Given Answer: 2456 (Abhilash Q55)
Uncaught exception terminates only the thread that caused exception not the JVM.
Do you agree?
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
rajsim,
An uncaught exception results in the uncaughtException() method of the thread's ThreadGroup being invoked. If the threadgroup is main(), it results in the termination of the program in which it is thrown.
Take a look at ThreadGroup.uncaughtException() method in Javadoc
API for more detailed account of what it does.

Ajith
[This message has been edited by Ajith Kallambella (edited July 19, 2000).]


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
In one the posts there was a question about threads and threadgroups, and I was tempted to make the assumption that if you dont explicitly assign a threadgroup to a thread, it belongs to a default threadgroup which again I assumed should be main .With these assumptions, I fell for the conclusion,which was an option in the question that every thread has at least a threadgroup associated with it.
I wonder if anyone knows for certain whether the assumptions above are valid or not, an therefore whether the conclusion can be universally endorsed.
Herbert
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Herbert,
I am pretty sure these assumptions are correct. Threads by default belong to the ThreadGroups of the parent thread. Since main is the beginning of execution of your program, all threads belong to the main threadgroup unless explicitly assigned.
Any contestants?
Ajith
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
I thought that when an uncaught exception for a thread was thrown, it results in the termination of the program regardless of the ThreadGroup it belongs to. Could someone explain this to me?
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Marcela,
Just like Threads have parent Threads, ThreadGroups have parent ThreadGroups. The uncaughtException method call propagates to the ThreadGroup sitting at the top of the hierarchy, and gives a chance for any of these classes to handle the exception. If no such handler is found, then the method uncaught-Exception is invoked for the ThreadGroup that is the parent of the current thread-thus every effort is made to avoid letting an exception go unhandled. Incase of a ThreadGroup that has no parent groups, the method simply terminates.
You can create your own custom ThreadGroups that override the uncaughtException() method if you want to handle uncaught exceptions in a certain way.
Here is a code snippet which ( hopefully ) makes things clear


Ajith
rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116
Some code to show JVM doesn't exit.

output:
d = 10 ThreadGroup main
1 Runner 10
d = 0 ThreadGroup main
java.lang.ArithmeticException: / by zero
at Runner.run(Test.java:8)
at java.lang.Thread.run(Thread.java:484)
1 Runner 10
1 Runner 10
..
..
1 Runner 10
One thread dies after divide by 0. The other thread continues.
Both of them belong to "main" group.
From the "uncaughtException" API
...
Called by the Java Virtual Machine when a thread in this
thread group stops because of an uncaught exception.
...
I interpret this as:
An uncaught exception causes a thread to die and then the uncaughtException
method is called in the ThreadGroup.
However if other user threads are still running, the JVM
should keep running.
Do you agree?

[This message has been edited by rajsim (edited July 19, 2000).]
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Yes, only the culprit thread dies which failed to catch the exception. As for the JVM, as long as there are some user-threads running, it will not exit.
Ajith
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
Thanks Ajith and Rajsim. I now understand that an uncaught exception ends the Thread that throws it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When does the JVM exit?