• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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.
 
Bring out your dead! Or a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic