Pages

Program to implement sleep() method

/* Program to implement sleep() method */
class A extends Thread
{
public void run()
 {
try{
         sleep(3000);
      }catch(Exception e){}
  }
for(int i=1;i<=10;i++)
  {
   System.out.println("Inside A   " + i);
  }
}// end of thread A
class B extends Thread
{
 public void run()
 {
  try{
         sleep(1500);
      }catch(Exception e){}
  }
 for(int i=1;i<=10;i++)
 {
 System.out.println("Inside B   " + i);
}
} // end of Thread B
class C extends Thread
{
 public void run()
 {
for(int i=1;i<=10;i++)
System.out.println("Inside C   " + i);
 }
}// End of Thread C
class ThreadDemo2
{
 public static void main(String args[])
 {
 A a1=new A();
 B b1=new B();
 C c1=new C();
 a1.start();
 b1.start();
 c1.start();
 }
} // end of ThreadDemo2

-> In any case, when you execute the above program, you observe that Thread C starts execution first, then Thread B and finally Thread A starts its execution.