01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package horstmann.ch09_queue1;
/**
   An action that repeatedly inserts a greeting into a queue.
 */
public class Producer implements Runnable
{
  /**
      Constructs the producer object.
      @param aGreeting the greating to insert into a queue
      @param aQueue the queue into which to insert greetings
      @param count the number of greetings to produce
   */
  public Producer(String aGreeting, BoundedQueue<String> aQueue, int count)
  {
    greeting = aGreeting;
    queue = aQueue;
    greetingCount = count;
  }

  public void run()
  {
    try
    {
      int i = 1;
      while (i <= greetingCount)
      {
        if (!queue.isFull())
        {
          queue.add(i + ": " + greeting);
          i++;
        }
        Thread.sleep((int) (Math.random() * DELAY));
      }
    }
    catch (InterruptedException exception)
    {
    }
  }

  private String greeting;
  private BoundedQueue<String> queue;
  private int greetingCount;

  private static final int DELAY = 10;
}