• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

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: 23923
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.
reply
    Bookmark Topic Watch Topic
  • New Topic