Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dead Thread !!

 
Rao Rekha
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a small learning example here that I wrote to check if a thread is alive after putting it to sleep for 3 secs. But unfortunately the isAlive() method isn't returning true. Why is this thread dead ???

class threads implements Runnable
{
public void run()
{
// do something here..
}
}
class threadtest
{
public static void main(String args[])
{
Runnable r = new threads();
Thread t = new Thread(r);
t.start();
try{
t.sleep(3000);
}
catch(InterruptedException e)
{}


if(t.isAlive())
System.out.println("This is in a thread " + Thread.currentThread().getName());
}
}
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sleep() is a class method. So your t.sleep is really calling Thread.sleep(), but in the context of your main thread. So it is your main thread that is sleeping. The thread t that you created has long finished when you do the isAlive() call. I'll let you figure out where the Thread.sleep should go to get the result you want.
Also, what do you expect to be printed when t.isAlive() returns true? Exactly which thread's name are you going to print?.

-Barry
[ September 05, 2002: Message edited by: Barry Gaunt ]
 
Rao Rekha
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response Barry. Being new to this Thread concepts, I am not sure about my learning example. But I was expecting it to print "main". Does this mean that, during execution, there will be 2 different threads? Also, I didnt quite get what you said - "sleep() is a class method. So your t.sleep is really calling Thread.sleep(), but in the context of your main thread. So it is your main thread that is sleeping. The thread t that you created has long finished when you do the isAlive() call. " Could you please explain further? Thanks
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there are two threads. One running your main method and the other you have created with the new Thread(r) call. sleep() is a static method of the Thread class, and when you called t.sleep() you called Thread.sleep() on the main thread, not on the thread t.
You said that the println would print "main" that would be correct.
Try putting that sleep call in the run method (//do something here) and see what happens.
-Barry
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the user thread that's asleep and it's dead by the time the main thread checks if t (the user thread) is still alive). You can fool around with the code to check this...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic