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