import java.util.*; public class BoundedBuffer implements IBoundedBuffer { final int NUM_BUFFER; volatile int count_current_elements = 0; Object _lock = new Object(); ArrayList _buffer=new ArrayList(); public BoundedBuffer(int size) { NUM_BUFFER=size; } public void write(T item) throws InterruptedException{ synchronized(_lock) { while(count_current_elements>=NUM_BUFFER){ try { _lock.wait(); } catch(InterruptedException ie) { /* ignore */ } } _buffer.add(item); count_current_elements++; _lock.notifyAll(); } } public T read() throws InterruptedException{ T value; synchronized(_lock) { while(count_current_elements<=0){ try { _lock.wait(); } catch(InterruptedException ie) { /* ignore */ } } value =(T)(_buffer.remove(0)); count_current_elements--; _lock.notifyAll(); } return value; } }