001package horstmann.ch09_queue3; 002/** 003 An action that repeatedly inserts a greeting into a queue. 004 */ 005public class Producer implements Runnable 006{ 007 /** 008 Constructs the producer object. 009 @param aGreeting the greating to insert into a queue 010 @param aQueue the queue into which to insert greetings 011 @param count the number of greetings to produce 012 */ 013 public Producer(String aGreeting, BoundedQueue<String> aQueue, int count) 014 { 015 greeting = aGreeting; 016 queue = aQueue; 017 greetingCount = count; 018 } 019 020 public void run() 021 { 022 try 023 { 024 int i = 1; 025 while (i <= greetingCount) 026 { 027 queue.add(i + ": " + greeting); 028 i++; 029 Thread.sleep((int) (Math.random() * DELAY)); 030 } 031 } 032 catch (InterruptedException exception) 033 { 034 } 035 } 036 037 private String greeting; 038 private BoundedQueue<String> queue; 039 private int greetingCount; 040 041 private static final int DELAY = 10; 042}