• 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

Why is it legal to call join twice in the same context

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



My question is that Should I not get a IllegalThreadStateException When join is called again ?

That does not happen though, the code compiles without error and runs fine as expected (Main ended Printed after the loop ends).

If some one could explain this behavior I would be very grateful

--Thank you
 
Jai Mani
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way I looked at the source for join turns out it calls wait internally ?

This raises more questions than it answers


join() just calls join(0)

I'm absolutely stumped ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jai Mani wrote:My question is that Should I not get a IllegalThreadStateException When join is called again ?


No, why would you expect that? The API documentation of Thread.join() doesn't say it will ever throw IllegalThreadStateException.

What join() does is make the current thread wait until the thread that it's joining has finished. If the other thread has already finished, the call returns immediately. It doesn't throw an exception when the thread you are joining has already finished.
 
Jai Mani
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jesper de Jong, that clears up a lot of stuff.

But I have a new question now

Inside the join() method wait() is called, what about its exceptions Where are they handled ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jai Mani wrote:Thank you Jesper de Jong, that clears up a lot of stuff.

But I have a new question now

Inside the join() method wait() is called, what about its exceptions Where are they handled ?



Which exceptions are you referring to? The exceptions from the wait() method, or any unhandled exceptions from the running thread?

For the first case, the wait() method and the join() method has the same "throws" signature, so the wait() method exception get propagated up. For the second case, any unhandled exceptions from the thread goes to the threads uncaught exception hander -- which can set using the Thread class.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jai Mani wrote:By the way I looked at the source for join turns out it calls wait internally ?

This raises more questions than it answers


join() just calls join(0)

I'm absolutely stumped ?




Yes, the join() method calls the wait() method -- and there is a notifyAll() call as part of the thread shutdown mechanism (after the thread is marked as no longer alive). This means that you should never use the Thread object as a wait/notify object, as the Java core library is using it -- at least with the current implementations.

Henry

 
Jai Mani
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again Henry, I was getting curious about that point.

Thank you again , This resolved my query.
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic