Thread class provides join() method. It is invoked by one thread on other thread to wait for the other thread to complete before proceeding. It will block the calling thread from becoming runnable till the other thread is alive. As shown in program below.
Implementation:// Joining_ok.java class Task extends Thread { Task(String name) { setName(name); } public void run() { for(int i=1;i<=2;i++) { System.out.println(“thread->”+getName()+” : “+i); } System.out.println(getName()+ “ : thread Finished....”); } } public class Joining_ok { public static void main(String[] args) { System.out.println(“Main thread Started...”); Task thrd=new Task(“Real Java”); thrd.start(); try { thrd.join(); } catch(Exception ex) { ex.printStackTrace(); } System.out.println(“Main thread Finished...”); } }
Output Main thread Started... thread->Real Java : 1 thread->Real Java : 2 Real Java : thread Finished.... Main thread Finished...