01: /**
02: An action that repeatedly inserts a greeting into a queue.
03: */
04: public class Producer implements Runnable
05: {
06: /**
07: Constructs the producer object.
08: @param aGreeting the greating to insert into a queue
09: @param aQueue the queue into which to insert greetings
10: @param reps the number of repetitions
11: */
12: public Producer(String aGreeting, Queue aQueue, int reps)
13: {
14: greeting = aGreeting;
15: queue = aQueue;
16: repetitions = reps;
17: }
18:
19: public void run()
20: {
21: try
22: {
23: int i = 1;
24: while (i <= repetitions)
25: {
26: queue.add(i + ": " + greeting);
27: i++;
28: Thread.sleep((int)(Math.random() * DELAY));
29: }
30: }
31: catch (InterruptedException exception)
32: {
33: }
34: }
35:
36: private String greeting;
37: private Queue queue;
38: private int repetitions;
39:
40: private static final int DELAY = 10;
41: }