FileDocCategorySizeDatePackage
ProdCons1.javaAPI DocExample3417Sun Feb 08 21:34:08 GMT 2004None

ProdCons1

public class ProdCons1 extends Object
Producer-Consumer in Java. Version 1.

Fields Summary
protected LinkedList
list
Constructors Summary
Methods Summary
protected voidconsume()

		Object obj = null;
		int len = 0;
		synchronized(list) {
			while (list.size() == 0) {
				try {
					list.wait();
				} catch (InterruptedException ex) {
					return;
				}
			}
			obj = list.removeLast();
			len = list.size();
		}
		System.out.println("Consuming object " + obj);
		System.out.println("List size now " + len);
	
public static voidmain(java.lang.String[] args)

		ProdCons1 pc = new ProdCons1();
		System.out.println("Ready (p to produce, c to consume):");
		int i;
		while ((i = System.in.read()) != -1) {
			char ch = (char)i;
			switch(ch) {
				case 'p":	pc.produce(); break;
				case 'c":	pc.consume(); break;
			}
		}
	
protected voidproduce()


	   
		int len = 0;
		synchronized(list) {
			Object justProduced = new Object();
			list.addFirst(justProduced);
			len = list.size();
			list.notifyAll();
		}
		System.out.println("List size now " + len);