/*sleep and join method join(): It will put the current thread on wait until the thread on which it is called is dead. If a thread is interrupted then it will throw InterruptedException.*/ class Even extends Thread{ public void run(){ int i; try{ for(i=0;i<=10;i++){ if(i%2==0) System.out.print("Even="+ i +" " ); this.sleep(500);}} catch(InterruptedException e){ System.out.println(e);} }} class Odd extends Thread{ public void run(){ int i; try{ for(i=0;i<=10;i++){ if(i%2!=0) System.out.print("Odd="+ i +" " ); this.sleep(500);}} catch(InterruptedException e){ System.out.println(e);} }} class Mythread4{ public static void main(String a[]){ Thread t1 = new Even(); Thread t2 = new Odd(); try{t1.start(); t1.join(); t2.start(); t2.join(); int i; for(i=0;i<=10;i++){ System.out.print("Main:"+ i +" " ); Thread.sleep(500);}} catch(InterruptedException e){ System.out.println(e);} } }