File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Java thread - calling start() 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 "Java thread - calling start() " Watch "Java thread - calling start() " New topic
Author

Java thread - calling start()

Vipin Das
Ranch Hand

Joined: Jul 05, 2004
Posts: 47
In k&B it is said that an exception will be thrown, if the start() methos on a dead thread object
is invoked, is it right?
venkataraman muthuvel
Greenhorn

Joined: Jan 28, 2004
Posts: 22
Yes
if the start() method on a dead thread object
is invoked.

Exception will be throw is it Correct.
Amit G Piplani
Ranch Hand

Joined: Jul 21, 2004
Posts: 83
IllegalThreadStateException will be thrown when start() is called on an already dead thread.


SCJP,SCWCD,SCBCD,SCDJWS1.4, SCDJWS1.5
Fletcher Estes
Ranch Hand

Joined: Jul 01, 2004
Posts: 108
NO exception will be thrown if the start() method is invoked on a dead thread. An exception will only be thrown if you try to start a thread that is already running, but has not yet completed (died). Try the following code:


kavya krushi
Ranch Hand

Joined: Oct 25, 2000
Posts: 48
This is very interesting .... Fletcher, i have tried the code you had posted and found that even though no exceptions are thrown, the run method is not called again ... So i was wondering what exactly happens when the start methos is called the second time ....
George Koshy
Greenhorn

Joined: Jul 21, 2004
Posts: 8
Even if a thread is dead you can invoke all the methods on the thread , so in this case i believe start behaves like a normal method , but the JVM detects that this is a dead thread , so does not call run method. But i think this is undefined behaviour and might behave differently on different platforms.
Correct me if i am wrong.

Regards,
George
Fletcher Estes
Ranch Hand

Joined: Jul 01, 2004
Posts: 108
Originally posted by George Koshy:
But i think this is undefined behaviour and might behave differently on different platforms.

I think you can be fairly certain that the JVM will behave the same (as you described) on all platforms. One thing to note is that if you call the start() method on a dead thread, subsequent calls to isAlive() will return true even though the thread isn't really executing the run() method!

This is a condition you should be wary of when doing any thread programming

peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by kavya krushi:
This is very interesting .... Fletcher, i have tried the code you had posted and found that even though no exceptions are thrown, the run method is not called again ... So i was wondering what exactly happens when the start methos is called the second time ....


Here's a modified version of Fletcher's code that demonstrates start() in the 3 relevant states.


The text on page 505 of K&B is wrong and is corrected in the errata found at SCJP&SCJD for Java2. Even the best edited books have errata, most authors provide you with a page on the web nowadays, you should always read it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java thread - calling start()