FileDocCategorySizeDatePackage
ProdCons2.javaAPI DocExample5263Sun Feb 08 21:34:08 GMT 2004None

ProdCons2

public class ProdCons2 extends Object
Producer-consumer in Java, Take II.

Fields Summary
protected LinkedList
list
Throughout the code, this is the object we synchronize on so this is also the object we wait() and notifyAll() on.
protected int
MAX
protected boolean
done
Constructors Summary
ProdCons2(int nP, int nC)

		for (int i=0; i<nP; i++)
			new Producer().start();
		for (int i=0; i<nC; i++)
			new Consumer().start();
	
Methods Summary
public static voidmain(java.lang.String[] args)


		// Start producers and consumers
		int numProducers = 4;
		int numConsumers = 3;
		ProdCons2 pc = new ProdCons2(numProducers, numConsumers);

		// Let it run for, say, 10 seconds
		Thread.sleep(10*1000); 

		// End of simulation - shut down gracefully
		synchronized(pc.list) {
			pc.done = true;
			pc.list.notifyAll();
		}