• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DeadThreads

 
latha
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1467
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . 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
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ajith,
sometime your 2 cents worth of more than 20 cents. keep doing it.
regards
vivek
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic