You have thread T1, T2 and T3, how will you ensure that thread T2 run after T1 and thread T3 run after T2? You have three threads T1, T2, and T3. How can you run T2. after T1. after T2. after T2? The following is a possible implementation method:
public class ThreadJoin implements Runnable { private final Thread joinTarget; public ThreadJoin(Thread joinTarget) { this.joinTarget = joinTarget; } @Override public void run() { if(joinTarget != null) { try { joinTarget.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Join complete " + Thread.currentThread().getName()); } } private static class MyThread implements Runnable { @Override public void run() { System.out.println("MyThread"); } } public static void main(String[] args) { Thread t1 = new Thread(new MyThread(), "Thread1"); Thread t2 = new Thread(new ThreadJoin(t1), "Thread2"); Thread t3 = new Thread(new ThreadJoin(t2), "Thread3"); t1.start(); t2.start(); t3.start(); } }