How to Join Multiple Threads in Java - Thread Join Example

Join method from Thread class is an important method and used to impose order on execution of multiple Threads. The concept of joining multiple threads is very popular on a 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 illustrates 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 a 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 their task.
Read more »