public class Monitor { private int[] _data; private int _index; public Monitor(int capacity) { _data = new int[capacity]; _index = 0; } public synchronized void add(int number) { // room for more data? while ( _index >= _data.length ) { try { wait(); } catch (InterruptedException ie) { } } // add number and notify waiting threads _data[_index++] = number; notifyAll(); } public synchronized int get() { // is data to retreive? while ( _index <= 0 ) { try { wait(); } catch (InterruptedException ie) { } } // get number and notify waiting threads int out = _data[--_index]; notifyAll(); return out; } }