class ThDemo extends Thread { public void run() { int x; for(x=1;x<=3;x++) { try { Thread.sleep(2000); System.out.println(x); } catch(Exception e) { System.out.println(e); } } } } public class Main { public static void main(String args[]) { ThDemo d1=new ThDemo(); ThDemo d2=new ThDemo(); ThDemo d3=new ThDemo(); d1.start(); try { System.out.println("Current Thread: "+ Thread.currentThread().getName()); d1.join(); } catch(Exception e) { System.out.println(e); } d2.start(); try { System.out.println("Current Thread: "+ Thread.currentThread().getName()); d2.join(); } catch(Exception e) { System.out.println(e); } d3.start(); try { System.out.println("Current Thread: "+ Thread.currentThread().getName()); d3.join(); } catch(Exception e) { System.out.println(e); } } }
Current Thread: main
1
2
3
Current Thread: main
1
2
3
Current Thread: main
1
2
3