• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When does the JVM exit?

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ajith and Rajsim. I now understand that an uncaught exception ends the Thread that throws it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic