Wednesday, February 6, 2013

Join method from Thread class is an important method and used to impose
order on execution of multiple Threads. Concept of joining multiple threads is
very popular on mutithreading interview question.
Here is one of such question, “You have three threads T1, T2 and T3, How do you
ensure that they finish in order T1, T2, T3 ?. This question illustrate power
of join method on multithreaded programming. Unlike classical thread questions
like difference between wait and sleep method
or solving producer consumer problem in Java,
This one is bit tricky. You can do this by using join method, by calling
T1.join() from T2 and T2.join() from T3. In this case thread T1 will finish first, followed by T2 and T3. In this Java multithreading tutorial
we will have a closer look on join method with a simple example. Idea is to
illustrate how join method works in simple words. By the way from Java 5
onwards you can also use CountDownLatch and CyclicBarrier classes to
implement scenarios like one thread is waiting for other threads to finish there
task.

When to use join method in Java

As I said join is an important and useful method from Thread class but many times
overlooked. Similar to wait method, by using join
method, we can make one Thread to wait for another. Primary use
of Thread.join() is to wait for another thread and start execution
once that Thread has completed execution or died. Join is also a blocking method, which blocks
until thread on which join has called die or specified waiting time is over. By
the way understanding, how join method works, is not straight forward. Many
developers get confused on things like, which thread will wait, which thread will join etc. These points will
be more clear when we go through an example
of joining multiple Thread in Java using join() method.

Thread Join Example in Java

Here is a simple example of joining two threads using Thread.join() method. By
the way unlike Thread.sleep() method, join() is not a static method, it needs to be
call on a java.lang.Thread object. Current thread, which
calls join method will wait until thread on which join has called die or
wait at most specified millisecond for this thread to die.

Output:
main is Started
Thread-0 is Started
Thread-0 is Completed
main is Completed

If you look at above example, first main thread is started and
than it creates another thread, whose name is "Thread-0" and
started it. Since Thread-0 sleep for 2 seconds, it require at least 2 seconds to complete and in between main thread called join method on
Thread-0 object. Because of join method, now main thread will wait until Thread-0 completes
its operation or You can say main thread will join Thread-0. If you
look on output, it confirms this theory.

Important
point on Thread.join method

Now we know How to use join method in Java, it’s time to see some
important points about Thread.join() method.

That’s all on How to join two Threads in Java with example. You
can Join multiple threads by using Thread.join() method. Join
is particularly useful to make one thread wait for other, or serializing two
function e.g. first load your cache and than start processing request.

I was reading this article and i fount it very useful. I have this task: To start service D, service A and B need to be startedTo stop service A, service B, D and C must be stopped firstService B and C can be started in parallel immediately after A has started. Conversely, they can stop in parallel.

Do you have nay suggestions how can i solve it? I'm new in java so i don't know so much things but any advice, suggestions would be useful.

at #1 t1 started by main threadat #2 main thread joined after t1, means main thread will wait for t1 to completeat #3 t2 started by main threadat #4 main thread joined after t2, means main thread will wait for t2 to complete

so up to line 4 main thread is waiting for both t1 and t2 to finish their executionbutIt doesn't mean that t2 waiting for t1 to complete, and same thing for line 5 and 6

at the end of line 6 main thread is waiting for t1, t2 and t3

So my question is how we can guaranty that t2 will execute after t1 and t3 will execute after t2 , because no where we its written like t3.join() from t2 and t2.join() from t1 ?

@AnonymousDoesn't main thread wait for t1 to finish execution before it even starts t2? So if main starts one thread and waits for it to terminate before starting the next thread, how is the sequence not guaranteed?