• 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

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: 23899
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