• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarify Use of the join method

 
d jones
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 21007
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic