File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why is it legal to call join twice in the same context Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Why is it legal to call join twice in the same context " Watch "Why is it legal to call join twice in the same context " New topic
Author

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

Jai Mani
Greenhorn

Joined: Sep 11, 2010
Posts: 23



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

Joined: Sep 11, 2010
Posts: 23
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
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jai Mani
Greenhorn

Joined: Sep 11, 2010
Posts: 23
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

Joined: Sep 28, 2004
Posts: 16695
    
  19

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19

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

Joined: Sep 11, 2010
Posts: 23
Thanks again Henry, I was getting curious about that point.

Thank you again , This resolved my query.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Why is it legal to call join twice in the same context
 
Similar Threads
deal all, my second mock exam is ready, please try it.
Doubt in Thread run method.
How to make a main thread wait until all other threads die out?
Query about start() and join()
Salient join() characteristics