• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java thread - calling start()

 
Vipin Das
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes
if the start() method on a dead thread object
is invoked.

Exception will be throw is it Correct.
 
Amit G Piplani
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IllegalThreadStateException will be thrown when start() is called on an already dead thread.
 
Fletcher Estes
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic