Source of Producer.java


  1: /**
  2:    An action that repeatedly inserts a greeting into a queue.
  3: */
  4: public class Producer implements Runnable
  5: {
  6:    /**
  7:       Constructs the producer object.
  8:       @param aGreeting the greating to insert into a queue
  9:       @param aQueue the queue into which to insert greetings
 10:       @param count the number of greetings to produce
 11:    */
 12:    public Producer(String aGreeting, BoundedQueue<String> aQueue, int count)
 13:    {
 14:       greeting = aGreeting;
 15:       queue = aQueue;
 16:       greetingCount = count;
 17:   }

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

 39:    private String greeting;
 40:    private BoundedQueue<String> queue;
 41:    private int greetingCount;

 43:    private static final int DELAY = 10;
 44: }