Listing 3: File Independent3.java: Uses the Thread.sleep method

class MyThread extends Thread
{
   private int delay;
   private int count;
   public MyThread(String msg, int delay, int count)
   {
      super(msg); // Optional thread name
      this.delay = delay;
      this.count = count;
   }
   public void run()
   {
      for (int i = 0; i < count; ++i)
      {
         try
         {
            Thread.sleep(delay);
            System.out.println(getName());
         }
         catch (InterruptedException x)
         {
            // Won't happen in this example
         }
      }
   }
}

class Independent3
{
   public static void main(String[] args)
   {
      Thread t1 = new MyThread("DessertTopping", 100, 8);
      Thread t2 = new MyThread("FloorWax", 200, 4);
      t1.start();
      t2.start();
   }
}

/* Output:
DessertTopping
FloorWax
DessertTopping
DessertTopping
FloorWax
DessertTopping
DessertTopping
FloorWax
DessertTopping
DessertTopping
FloorWax
DessertTopping
*/
— End of Listing —