• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Jai Mani
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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
  • 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 ?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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 ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks again Henry, I was getting curious about that point.

Thank you again , This resolved my query.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!