public class Semaphore { int x; public Semaphore (int x) { this.x = x; } public synchronized void P () { while (x == 0) try { wait(); } catch (java.lang.InterruptedException e) { }; x = x - 1; } public synchronized void V () { x = x + 1; notify(); } }