This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes A doubt on Multiple Threads and the Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A doubt on Multiple Threads and the "join()" method !!" Watch "A doubt on Multiple Threads and the "join()" method !!" New topic
Author

A doubt on Multiple Threads and the "join()" method !!

radhakrishnan jankiraman
Greenhorn

Joined: Nov 25, 2001
Posts: 3
Hi !!
I understand muli-threading as "parallel instances of execution" and i have a query in this regard. The following is a code and its output. I would like to how will you describe "what exactly is the join() method doing here?".

And the output is:
New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
Thread One is Alive:true
Thread Two is Alive:true
Thread Three is Alive:true
Waiting for threads to finish.
One:5
Two:5
Three:5
One:4
Two:4
Three:4
One:3
Two:3
Three:3
One:2
Two:2
Three:2
One:1
Two:1
Three:1
Finished all the threads!!

Now is'n't it misleading to say that " join() waits until the thread on which it is called terminates. " ?
What it actually does is that it does not let the thread "from which it is being called" terminate,
till the thread "on which it is being called" terminates
The important thing to note is that after a call to join() in main, the control passes to the next join() and so on.
But it does not come out of the try{}catch{} block , as is evident from the last print statement. Thats seems like "magic".
Please enlighten me on this.
Thanx and Regards !!
radhakrishnan. j
(edited by Cindy to format code)
[This message has been edited by Cindy Glass (edited November 28, 2001).]
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by radhakrishnan jankiraman:
Now is'n't it misleading to say that " join() waits until the thread on which it is called terminates. " ? What it actually does is that it does not let the thread "from which it is being called" terminate, till the thread "on which it is being called" terminates
The second description would certainly be more misleading than the first. The join() call doesn't merely prevent the calling thread from terminating - it simply blocks until the Thread it is called on terminates. This is also why your "Finished all the threads" message displays only when all three threads have terminated. The join() calls don't complete until their respective threads are dead.
- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A doubt on Multiple Threads and the "join()" method !!