/** * Represents a semaphore. * * @author Franck van Breugel */ public class Semaphore { private int value; /** * Initializes this semaphore with the given value. * * @param value the initial value of this semaphore. */ public Semaphore(int value) { super(); this.value = value; } /** * Tries to decrement the value of this semaphore. */ public synchronized void acquire() { while (this.value == 0) { try { this.wait(); } catch (InterruptedException e) { // do nothing } } this.value--; } /** * Increments the value of this semaphore. */ public synchronized void release() { this.value++; this.notifyAll(); } }