my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes isAlive() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "isAlive() method" Watch "isAlive() method" New topic
Author

isAlive() method

Cody Beth
Greenhorn

Joined: May 20, 2002
Posts: 11
Hi All,
Need to know about Thread.isAlive() method
As we know that we cannot re-start a thread once it has executed a run method, i.e., it is not alive after its dead.
Check the code below..

the output of the code is :
1 = false
2 = true // as start method called
In run method
3 = false // run method is completed
4 = true ??? This is showing the thread is alive but run method did not execute again.

Regards,
Cody
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Thanks Cody. That makes isAlive() method a bit less reliable.


SCJP2. Please Indent your code using UBB Code
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
But Jose, shouldn't second start give a runtime error?
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Barkat Mardhani:
But Jose, shouldn't second start give a runtime error?

That's a known bug in the JVM.
Kalai Ganesh
Ranch Hand

Joined: Sep 08, 2003
Posts: 32
Hai Cody,
When I executed the same program the output was
1= false
2= true
3= false
4= false
Probably the second start() didn't make the thread alive here. Why is this difference? Anyway this shouldn't be platform dependent. Am I right?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: isAlive() method
 
Similar Threads
myThread.isAlive() v.s. WeakReference ? URLyBird 1.2.2 using Socket
join() method in Thread doesn't work as expected
Thread Question??
Thread Question about sleep method
what could be the answer ...