developers

    Forum Stats

  • 3,873,880 Users
  • 2,266,627 Discussions
  • 7,911,650 Comments

Discussions

when to use join method

Sanket.Bhalerao
Sanket.Bhalerao Member Posts: 9
edited Aug 19, 2014 5:49AM in New To Java

Hi,

recently i have been studying Threading, and quite fascinated by it. however i am struggling to understand in what circumstance a programmer will be using a join method on a thread.

what i understand from reading docs is, if i call join() on a thread "thread1", then whenever this call happens the "current thread" will stop its execution till "thread1" finishes its execution i.e. DIE.

when code is executing no one has control over which thread will be the current thread when the join is called.

can someone please explain with a real life scenario?

Thanks in advance

Answers

  • recently i have been studying Threading, and quite fascinated by it. however i am struggling to understand in what circumstance a programmer will be using a join method on a thread.
    what i understand from reading docs is, if i call join() on a thread "thread1", then whenever this call happens the "current thread" will stop its execution till "thread1" finishes its execution i.e. DIE.
    

    There are plenty of examples and documentation on the internet about multithreading and the use of 'join'.

    As the doc quote suggests, and at the risk of stating the obvious, it is used when you want a thread to stop its own execution until another thread has finished its own execution.

    That is a form of 'serialization' that ensures that certains steps of a process are done in a desired order.

    The generic 'multi-threading' use case is that the threads can executing in parallel and have NO dependencies on each. For example, 10 threads each loading a specific file into the database.

    But there are times when one process is dependent on another. Rather than bind those two processes together (e.g. by using method calls from within a single thread) a more modular approach is to launch each process in a thread but cause the second process to wait, using 'join', until the first process/thread has completed.

    when code is executing no one has control over which thread will be the current thread when the join is called.

    I have NO IDEA what that even means.

    Again, stating the obvious, a thread can NOT execute the 'join' method unless that thread is active so if that thread is active is IS the 'current' thread. When it executes the 'join' it will be suspended until the 'joined' thread terminates. The thread that it is 'joining' may not become 'active' right away and it make take several thread rotations before it completes execution and reactivates the thread that 'joined' it.

  • jwenting
    jwenting Member Posts: 4,864 Gold Badge

    simple scenario:

    thread A needs the result of thread B before it can continue (say B does some background calculations) at some point during its execution.

    Thread A now joins on B, causing it to execute up to that point and then wait until it gets notified that B is done, at which point the results of B should be available and A can access them to complete its own work.

This discussion has been closed.
developers