Monitor Invariant - Buffer(2)

Monitor Invariant - Buffer(2)

synchronized public Object get() {

// [0 £ count £ size]

while (count==0) {… wait() …}

… // [0 < count £ size]

--count;

… // [0£count < size]

notify(); // [count

}

Both put() and get() use notify() rather than notifyAll() since count is only incremented or decremented by one and consequently, it is only necessary to resume execution of a single thread. There is no situation in which both producer and consumer threads are suspended on the condition queue.

ie. producer wait condition: [count == size]

consumer wait condition:[count == 0].

Previous slide Next slide Back to the first slide View Graphic Version