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


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

Dead Thread !!

Rao Rekha
Greenhorn

Joined: Jul 29, 2002
Posts: 11
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

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Rao Rekha
Greenhorn

Joined: Jul 29, 2002
Posts: 11
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Mar 22, 2002
Posts: 1055
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dead Thread !!
 
Similar Threads
gives IllegalThreadStateException why?
target Runnable of a thread
When both Thread and Runnable have public void run
regarding threads