Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java thread - calling start()

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes
if the start() method on a dead thread object
is invoked.

Exception will be throw is it Correct.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IllegalThreadStateException will be thrown when start() is called on an already dead thread.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ....
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic