public class CubbyHole{ private Object slot; public CubbyHole() { slot = null; } public synchronized void putIn( Object obj ) throws InterruptedException { print( "in putIn() - entering" ); while( slot!=null ) { print( "in putIn() - occupied, about to wait()" ); wait(); print( "in putIn() - notified, back from wait()" ); } slot = obj; print( "in putIn() - filled slot, about to notifyAll()" ); notifyAll(); print( "in putIn() - leaving" ); } public synchronized Object takeOut() throws InterruptedException { print( "in takeOut() - entering" ); while( slot==null ) { print( "in takeOut() - empty, about to wait()" ); wait(); print( "in takeOut() - notified, back from wait()" ); } Object obj = slot; slot = null; print( "in takeOut() - emptied slot, about to notifyAll()" ); notifyAll(); print( "in takeOut() - leaving" ); return obj; } private static void print( String msg ) { String name = Thread.currentThread().getName(); System.out.println( name + ":" + msg ); } }/*********************************************************/
public class CubbyHoleMain{ private static void print( String msg ) { String name = Thread.currentThread().getName(); System.out.println( name + ":" + msg ); }
public static void main(String[] args) { final CubbyHole ch = new CubbyHole(); Runnable runA = new Runnable() { public void run() { try { String str; Thread.sleep( 500 ); str = "multithread"; ch.putIn( str ); print( "in run() - just put in: '"+ str + "'" ); str = "programming"; ch.putIn( str ); print( "in run() - just put in: '" + str + "'" ); str = "with Java"; ch.putIn( str ); print( "in run() - just put in: '" + str + "'" ); }catch( InterruptedException x ) { x.printStackTrace(); } } } ; / Runnable runB = new Runnable() { public void run() { try { Object obj; obj = ch.takeOut(); print( "in run() - just took out: '" + obj + "'" ); Thread.sleep( 500 ); obj = ch.takeOut(); print( "in run() - just took out: '" + obj + "'" ); obj = ch.takeOut(); print( "in run() - just took out: '" + obj + "'" ); }catch( InterruptedException x ) { x.printStackTrace(); } } } ; Thread threadA = new Thread( runA, "threadA" ); threadA.start(); Thread threadB = new Thread( runB, "threadB" ); threadB.start(); } }/****************************************************/
threadB:in takeOut() - enteringthreadB:in takeOut() - empty, about to wait()threadA:in putIn() - enteringthreadA:in putIn() - filled slot, about to notifyAll()threadA:in putIn() - leavingthreadB:in takeOut() - notified, back from wait()threadB:in takeOut() - emptied slot, about to notifyAll()threadB:in takeOut() - leavingthreadB:in run() - just took out: 'multithread'threadA:in run() - just put in: 'multithread'threadA:in putIn() - enteringthreadA:in putIn() - filled slot, about to notifyAll()threadA:in putIn() - leavingthreadA:in run() - just put in: 'programming'threadA:in putIn() - enteringthreadA:in putIn() - occupied, about to wait()threadB:in takeOut() - enteringthreadB:in takeOut() - emptied slot, about to notifyAll()threadB:in takeOut() - leavingthreadB:in run() - just took out: 'programming'threadB:in takeOut() - enteringthreadB:in takeOut() - empty, about to wait()threadA:in putIn() - notified, back from wait()threadA:in putIn() - filled slot, about to notifyAll()threadA:in putIn() - leavingthreadA:in run() - just put in: 'with Java'threadB:in takeOut() - notified, back from wait()threadB:in takeOut() - emptied slot, about to notifyAll()threadB:in takeOut() - leavingthreadB:in run() - just took out: 'with Java'