aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes DeadThreads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "DeadThreads" Watch "DeadThreads" New topic
Author

DeadThreads

latha
Greenhorn

Joined: Jul 17, 2002
Posts: 14
According to RHE(pg:204), once a thread is dead, it cannot be started again. But the dead thread continues to exist, and we can access its data and call its methods.
Can anybody explain me with an example, how we can access data in the dead thread without creating another thread instance.
Thanks in advance...
Savithri Devaraj
Ranch Hand

Joined: Jun 26, 2000
Posts: 103
Originally posted by latha:
According to RHE(pg:204), once a thread is dead, it cannot be started again. But the dead thread continues to exist, and we can access its data and call its methods.
Can anybody explain me with an example, how we can access data in the dead thread without creating another thread instance.
Thanks in advance...

A thread object is like any other object. If it has primitives/other data objects defined, they can be accessed. If it has other methods (other than the run()), they can be called with the object reference just like any other object. You just can't restart the thread as a process.
HTH,
Savithri
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Latha,
Just to give you an example. In this I am calling a method on a dead thread. Simillary you can access any other variables also.
regds
maha anna.
<pre>


class SampleThread extends Thread{
public void run() {
System.out.println("Hello Latha");
}
public void printMe() {
System.out.println("I am printing from printMe() method");
}
}
class Test {
public static void main(String[] args) {
SampleThread t1 = new SampleThread();
t1.start();
try{
Thread.sleep(1000);
}catch(Exception e){}
//Assuming the t1 thread has done its work by now
//it is a deadThread now.
t1.printMe(); // See! we can call methods on a dead thread object!

//t1.start(); // But this is NOT legal.
//Once started, you can't start again

}
}

Output

Hello Latha
I am printing from PrintMe() method
thomas
Ranch Hand

Joined: May 26, 2002
Posts: 79
. . . and you can even call the run() method after the thread has died i.e. you can run it like any other ordinary method.
Lancy Mendonca
Ranch Hand

Joined: Aug 08, 2000
Posts: 54
Hi MahaAnna,
I think all your explainations are fab. The fantastic thing is U supplement all your explainations with simple code. I had a small query from your above code:
Why is it not possible to call t1.start() on a dead thread


Sun Certified Java Programmer<BR>Oracle Certified DBA
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
It is just another rule of the game. Running a thread involves the thread scheduler to maintain a lot of state information about the thread itself. Once the thread has finished running, these state information needs to be discarded. Perhaps that's why Java doesnot allow you to restart a thread.
Infact it is not illegal to restart a dead thread. You will not get any runtime errors if you try to do so. It just will not behave the way you expect it to, ie., a new thread will not get started and the call to start() is ignored.
One thing you can do, however, is to create a new Thread object using the dead thread target. In the example code above, you can say
Thread t2 = new Thread(t1);
t2.start();
This is perfectly valid and it creates another thread.
Just adding my 2 cents worth...
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi ajith,
sometime your 2 cents worth of more than 20 cents. keep doing it.
regards
vivek
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DeadThreads