Listing 2: File Independent2.java: Uses the Thread.yield method

class MyThread extends Thread
{
   private int count;
   public MyThread(String msg, int count)
   {
      super(msg); // Optional thread name
      this.count = count;
   }
   public void run()
   {
      for (int i = 0; i < count; ++i)
      {
         System.out.println(getName());
         Thread.yield();
      }
   }
}

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

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