import ch05.queues.*; public class ProducerConsumer { public static final int N = 1000000; public static final int QUEUESIZE = 500; public static final double HEAD_PROBABILITY = 0.5; public static void main (String[] args) { BoundedQueueInterface queue = new ArrayBndQueue(QUEUESIZE); int producerBlocked = 0, consumerBlocked = 0; for(int i=0; i queue) { if (!queue.isFull()) queue.enqueue( (int)(10*Math.random()) ); else System.out.println ("Producer blocked"); } public static void consume(BoundedQueueInterface queue) { if (!queue.isEmpty()) System.out.println (queue.dequeue() + " dequeued"); else System.out.println ("Consumer blocked"); } public static boolean coinFlipIsHeads() { return Math.random() < HEAD_PROBABILITY; } } // Local Variables: // compile-command: "export CLASSPATH=/a/dale-dataStructures/bookFiles:. ; \ // javac ProducerConsumer.java && \ // java ProducerConsumer | grep blocked | wc -l && \ // java ProducerConsumer | grep dequeued | wc -l " // End: