Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You have to be odd to be #1 - Seuss. An odd little ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic