FileDocCategorySizeDatePackage
ProdCons1.javaAPI DocExample976Sun Apr 29 19:56:42 BST 2001None

ProdCons1

public class ProdCons1 extends Object

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();
		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);