Source of Consumer.java


  1: /**
  2:    An action that repeatedly removes a greeting from a queue.
  3: */
  4: public class Consumer implements Runnable
  5: {
  6:    /**
  7:       Constructs the consumer object.
  8:       @param aQueue the queue from which to retrieve greetings
  9:       @param count the number of greetings to consume
 10:    */
 11:    public Consumer(BoundedQueue<String> aQueue, int count)
 12:    {
 13:       queue = aQueue;
 14:       greetingCount = count;
 15:    }

 17:    public void run()
 18:    {
 19:       try
 20:       {
 21:          int i = 1;
 22:          while (i <= greetingCount)
 23:          {
 24:             if (!queue.isEmpty())
 25:             {
 26:                String greeting = queue.remove();
 27:                System.out.println(greeting);
 28:                i++;
 29:             }
 30:             Thread.sleep((int)(Math.random() * DELAY));
 31:          }
 32:       }
 33:       catch (InterruptedException exception)
 34:       {
 35:       }
 36:    }

 38:    private BoundedQueue<String> queue;
 39:    private int greetingCount;

 41:    private static final int DELAY = 10;
 42: }