File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
radhakrishnan jankiraman
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic