class BoundedSemaphore

class BoundedSemaphore

class BoundedSemaphore {

private int value_; private int bound_;

BoundedSemaphore (int initial, int bound)

{ value_ = initial; bound_ = bound;}

synchronized public void up() {

while (value_== bound_) {try {wait();}

catch (InterruptedException e){}}

++value_; notify();

}

synchronized public void down() {

while (value_== 0) {try {wait();}

catch (InterruptedException e){}}

--value_; notify();

}

}

This example bounds the upper value which the Semaphore may have by blocking up()when value_ equals bound_.

Demo

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