FileDocCategorySizeDatePackage
Counters.javaAPI DocExample1467Mon Oct 16 19:44:06 BST 2000None

Counters.java

// File: Counters.java
// T Balls : Oct 1999

// Counter objects that are implemented as Threads

class Counter1 extends Thread
{  public Counter1( int id )
   {  myId = id;
   }

   private int myId;
   private int count = 0;
   public void run()
   {  while( count < 1000000 )
      {  count++;
         if(  (count % 50000) == 0 )
         { System.out.println( "Thread: " + myId + " count --> " + count );
         }
     }
   }
}

class RunCounter1
{  public static void main( String[] args )
   {  Counter1[] counters = new Counter1[5];
      int i;
      for( i = 0; i < counters.length; i++ )
      {  counters[i] = new Counter1( i );
      }
      for( i = 0; i < counters.length; i++ )
      {  counters[i].start();
      }
   }
}

class Counter2 implements Runnable
{  public Counter2( int id )
   {  myId = id;
   }

   private int myId;
   private int count = 0;
   public void run()
   {  while( count < 1000000 )
      {  count++;
         if(  (count % 50000) == 0 )
         { System.out.println( "Thread: " + myId + " count --> " + count );
         }
      }
   }
}

class RunCounter2
{  public static void main( String[] args )
   {  Counter2[] counters = new Counter2[5];
      int i;
      for( i = 0; i < counters.length; i++ )
      {  counters[i] = new Counter2( i );
      }
      for( i = 0; i < counters.length; i++ )
      {  new Thread( counters[i] ).start();
      }
   }
}