hi These are the few doubts i have about daemon and user thread. has anyone come across simillar Q's in exams and got the ans right ?? 1.What happens when there is an uncaught exception in a daemon thread ? 2.What happens when there is an uncaught exception in a user thread ? 3. can daemon threads be grouped together ? 4. can daemon threads be terminated/killed ?
1.What happens when there is an uncaught exception in a daemon thread ? 2.What happens when there is an uncaught exception in a user thread ? The daemon thread terminates without effecting the other theads which is similar to what happens when there is an uncaught exception in a user threads. The thread ex below raizes a NullPointerException. This does not effect the printing out of "Hello World"
3. can daemon threads be grouped together ? Yes they can through thread groups. 4. can daemon threads be terminated/killed ? They can only be terminated/killed by stopping the work they are doing through an interrupt() method call and checking the boolean variable through Thread.isInterrupted() in the while loop in run(). Remember stop has been deprecated.
When a thread throws an exception and this exception is not caught then the thread group that owns that thread has the possibility of catching that exception by overriding the uncaughtException method of the ThreadGroup class. If that thread group can�t catch the exception the control is given to it�s parent thread group and if nobody can catches this the program ends. I don�t know if there are different behaviours of this between daemon and user threads, I think that no. In Rahul example the printing of Hello World depends on the begining of the run method of the thread, try slepping the main thread to ensure that the run method of the ex thread starts running you will see no Hello World printing out.
posted 19 years ago
Marcela , I disagree with you that
try slepping the main thread to ensure that the run method of the ex thread starts running you will see no Hello World printing out.
just because there is an exception in one thread in a ThreadGroup does not mean other threads stop execution. Check out the code below. Though the main thread sleeps and gives the ex thread a chance to run "Hello World" is being printed out after the exception details are printed out.
The output of the program is
C:\>java demo1 java.lang.NullPointerException at x.run(demo1.java:19) Hello World! Regds. Rahul P. Mahindrakar. [This message has been edited by Rahul Mahindrakar (edited September 11, 2000).]
posted 19 years ago
Rahul: Sorry for my late response, I tried the code and I got the output that you are telling me, but I read taht concept in a book and now I can�t find where, (I will seek more carefully to post the right answer). Perhaps what I said about the ThreadGroup parent class is the incorrect concept. Now I am in doubt about what happens, and I think is no as simple as trying that code because we are not trying the right example. I promiss I will post that concept ASAP.
posted 19 years ago
If code in a thread throws an unchecked exception and no code in that thread catches it then that thread (not necessarily the program) ends, whether the thread is a daemon or not. The JVM notifies the thread's group of the uncaught exception by calling its <code>uncaughtException( Thread t, Throwable e)</code> method. As implemented in <code>ThreadGroup</code>, this method just calls the same method of its parent <code>ThreadGroup</code>, if any. If the group has no parent, the method passes <code>ThreadDeath</code> errors to the JVM so that it may perform any necessary cleanup; for all other exceptions it just prints a stack trace. I think, but I'm not sure, that <code>ThreadDeath</code> is only thrown by <code>stop()</code> (which is, of course, deprecated). Sometimes its necessary to create your own subclass of <code>ThreadGroup</code> to do special cleanup when your threads die. The usual caution is to be sure to end your overriding <code>uncaughtException()</code> method with a call to the base class version, so that normal cleanup of threads may occur. If I'm right about <code>stop()</code> and <code>ThreadDeath</code>, the only consequence of not doing so will be the lack of a stack trace. I suppose I should write a program to test this. If anybody's interested, I'll post it later today. Thread groups may also be given daemon status, but there is no relation between the daemon status of a thread group and that of its threads. A daemon thread group is automatically destroyed when it contains no more threads or thread groups; you have to call <code>destroy()</code> explicitly to get rid of a non-daemon thread group. A Java program ends when all user threads have ended. The number of daemon threads still running makes no difference. This is the only difference between user and daemon threads.
posted 19 years ago
In JLS Second Edition - Chapter 11(the beginning): During the process of throwing an exception, the Java virtual machine abruptly completes, one by one, any expressions, statements, method and constructor invocations, initializers, and field initialization expressions that have begun but not completed execution in the current thread. This process continues until a handler is found that indicates that it handles that particular exception by naming the class of the exception or a superclass of the class of the exception. If no such handler is found, then the method uncaughtException 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. Additional and bad interpretations of the above are my responsability, apologize my earliest post.
I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad