• 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
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Clarify Use of the join method

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If we have


Here I would have thought that Thread b would stop running as soon as the line a.join() was executed.
But it doesn't it continues to run.

Could anyone please explain to me how the join method is working in this example? Shouldn't the a.join() line have caused any executing threads to be halted and not run again until a completed?

I understand if I have 1 thread and start it in the main method that it puts the main method on hold until the thread on which I call join completes.

But what if we wanted 1 thread to run after another, as in the above code, where I want thread b to run only when thread a has completed?

Many Thanks
 
author
Posts: 23942
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a thread calls the join() method of a thread object -- it will wait until the thread represented by that thread object completes. Nothing else is affected.

In your example, the main thread will wait until thread A completes. Period. Thread B (or any other thread) does not wait til thread A completes. Nor does the main thread waits for any other thread to complete.

Henry
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Main is a controlling thread, so you can delay the starting of thread b until the main thread has joined with thread a.


You could also pass thread a to thread b and have thread b join thread a.
 
You guys haven't done this much, have ya? I suggest you study this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic