Thursday, June 19, 2008

Thread problem - what'll 'threadObject = null' do for an active thread?


A thread problem - what will 'threadObject = null' do for an active thread?

package test;
public class Thread1 extends Thread{
public void run() {
int counter = 0;
while(true){counter++;
System.out.println("Inside Thread1 - " + counter);}
}
}


package test;
public class Class2 {
public static void main(String[] args) {
Thread1 thread1 = new Thread1();
thread1.start();
thread1 = null;
}
}

What will be the output when Class2 is run? 'thread1 = null' - what will this do?


Outout:

Inside Thread1 - 1
Inside Thread1 - 2
Inside Thread1 - 3
....
..... till the stack overflow occurs or till you forcibly terminate :-)

Did you anticipate anything else? Except the statement 'thread1 = null', everything else is pretty straightforward - the main() method will start the execution in the 'main' thread and during the execution, it'll create a new thread 'thread1', which will start its execution in a separate thread of execution. The 'main' thread being the parent thread will keep waiting for the child thread 'thread1' to finish its execution and only then the 'main' thread will exit. The child thread 'thread1' is running an infinite loop in its run() method, so it'll keep executiing unless it's forcibly terminated either by user's intervention OR may be due to stack overflow.

Okay... so if we include the statement 'thread1 = null' then why do you expect something else to happen? Maybe because you think that assigning 'null' to an object will simply make that object eligible for garbage collection (as in this case thread1 will have no other active references) and hence the thread should stop execution. Well... threads don't get terminated that way.

In Java, the JVM continues executing a thread until either of the following three occurs:-

  • Runtime.exit() method (or its equivalent System.exit() method) is called and the underlying Security Manager has permitted the method to execute. This invokes the JVM Shutdown Sequence (Read More in this article - JVM Shutdown Sequence >>) which eventually halts the entire JVM process (the thread under execution will just be a part of this process).
  • Runtime.halt() method is called and the underlying Security Manager is okay with the call. This abruptly and immediately halts the entire JVM process.
  • The thread under execution has returned from its run() method either by completing the execution of this method OR by thowing an uncaught exception which propagates beyond the run() method. A parent thread can return only when all of its child threads have already returned.

Now we can easily understand that 'thread1 = null' statement will not terminate the thread and the main thread will keep executing and eventually keep waiting (as thread1 is in infinite loop) for any of the above three cases to occur for the thread1 to terminate and after that the 'main' thread will also get terminated. In our example, we're not explicitly calling Runtime.exit() or Runtime.halt(), so they may be called only if we log off the machine or shut it down. Otherwise, only third case will cause the thread to terminate. run() being in an infinite loop will never complete its execution, so only an uncaught exception can terminate this thread. This uncaught exception can be either due to stack overflow OR may be due to some user intervention. Thus, we can say that having 'thread1 = null' will not at all affect the execution of any of the threads. The output will remain the same with without having this statement as well.



Share/Save/Bookmark


No comments: