• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Join method on thread

 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Can some one explain the odd behaviour in threads.
I thought that calling start method two times should throw IllegalThreadStateException. How ever the following code compiles fine and
executes fine.
Another interesting point I observed is the run method is called only once.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems that start() does not behaves in the same manner before a join as after it.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sarma,
I think that the reason why the exception error did not occur is because the start method has already returned (the thread is already dead).
Try removing the t.join method and replace it with t.start();
Hope this helps,
Maria
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I noticed is that it doesn't need to be a join but any method that ensures a delay long enough to let the started thread finish (sleep or wait). I guess Maria has got a point. The exeption did not occur because the thread was already dead.
Although I've seen stated that the exeption should occur if one calls start on a dead thread it looks like the exception only occurs if one calls start on a live thread.
The following code have the same behavior as the previous ones and run is called only once. If you comment the sleep line you'll get the error.

 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has been my experience that an IllegalThreadStateException is NOT thrown if the start method is invoked on a dead thread. However, the exception is thrown if the thread is still running when the start method is invoked.
For the purposes of the exam it is important to know that the javadoc for the Thread class states that the IllegalThreadStateException is thrown if the start method is invoked on a thread that has already been started.
The exam does not test your knowledge of undocumented behavior. Anytime there is a conflict between your practical experience and the documented behavior of Java then you should answer based on the documented behavior and not your actual experience.
Your practical experience tells you that an IllegalThreadStateException is not thrown when the start method is invoked on a dead thread, but the documentation tells you that an exception is thrown when the start method is invoked on a thread that has already been started. If your exam contains a question where the start method is invoked on a thread more than once, then the correct answer will involve the IllegalThreadStateException.
Remember, the exam prefers documentation over actual experience. The reasoning is that undocumented behavior can disappear at anytime so you should not write your code based on the assumption that undocumented behavior will become a defacto standard.
 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic