javatutelearn.com
Easy Learning
Search Javatutelearn.com :
public class joinexamp extends Thread { public void run(){ for(int i=0;i<5;i++) { System.out.println("current executing thread is "+Thread.currentThread().getName()); } } public static void main(String args[]) { joinexamp t0 = new joinexamp(); joinexamp t1 = new joinexamp(); joinexamp t2 = new joinexamp(); t0.start(); t1.start(); try { t0.join(); } catch (Exception e) { System.out.println("Exception raised "+e); } System.out.println("t0 is finished now"); t2.start(); } }Note:- Use join() method in try-catch block.
current executing thread is Thread-1 current executing thread is Thread-1 current executing thread is Thread-0 current executing thread is Thread-1 current executing thread is Thread-1 current executing thread is Thread-1 current executing thread is Thread-0 current executing thread is Thread-0 current executing thread is Thread-0 current executing thread is Thread-0 t0 is finished now current executing thread is Thread-2 current executing thread is Thread-2 current executing thread is Thread-2 current executing thread is Thread-2 current executing thread is Thread-2