Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Dead Thread !!

 
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());
}
}
 
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
 
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...
 
it's a teeny, tiny, wafer thin ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic