I am trying to understand the state of a Thread object after the thread has completed. I read in a book that once a thread has completed its run() method, the thread object will still be a valid object on the head and can still be used as an instance object and you can still call methods on it. I decided to try this with the following example:
class DieThread implements Runnable{
int y = 0;
public void run(){
for(int x=0; x<100; x++){
System.out.println(" " + Thread.currentThread().getName());
System.out.println("y is " + y);
y++;
System.out.println("y is " + y);
}
}
}
class ZiggyTest2{
public static void main(String[] args){
DieThread d = new DieThread();
Thread a = new Thread(d);
Thread b = new Thread(d);
Thread c = new Thread(d);
a.start();
b.start();
c.start();
try{
a.join();
b.join();
c.join();
}catch(Exception e){
System.out.println(e);
}
System.out.println("Running C's Run");
c.run();
}
}
main() waits for the other 3 threads to complete by using join(). It then call c’s run() method but for some reason it is not printing out anything from the run() method. Here is the last few lines of the output after i run the above program.
y is 287
y is 287
y is 288
y is 289
Thread-1
Thread-0
y is 289
y is 289
y is 290
y is 291
Thread-1
Thread-0
y is 291
y is 291
y is 292
y is 293
Thread-1
Thread-0
y is 293
y is 293
y is 294
y is 295
Thread-0
y is 295
y is 296
Thread-0
y is 296
y is 297
Thread-0
y is 297
y is 298
Thread-0
y is 298
y is 299
Thread-0
y is 299
y is 300
Running C's Run
Even though the thread has completed, i was expecting the value of y to be 400 because of the c.run() call.
Any ideas?
Thanks
The reason for what you are observing is buried in the implementation of the
Threadclass.When you call
c.run()you are calling therunmethod on the thread object. This is specified as calling therunmethod of thetargetobject (i.e. the runnable supplied in theThreadconstructor) if it is notnull. Iftargetisnullthen theThread.run()method does nothing.OK, so you supplied a non-null
target.In fact, when a
Threadfinishes, theThread.exit()method is called to clean up. And one of the things that the cleanup does is to assignnulltotarget. (The code has a comment about needing to aggressively clear references, and a bug id. Unfortunately, the corresponding bug report is missing, so it is not possible to know the real reason(s) that they do this.)Anyway, the bottom line is that you can’t rerun the target object’s
run()method by callingThread.run(). But you can call the target object’srun()method directly … if you have the target reference.