public class BSem {
    int value;

    // Constructor. If parameter is 0, then semaphore is initally zero
    // Otherwise, it is one.
    public BSem(int initial) {
        if (initial == 0) value = 0;
        else value = 1;
    }

    public synchronized void P( ) {
        while (value == 0) 
            try { wait(); }
            catch (java.lang.InterruptedException e) { };
        value = 0;
    }

    public synchronized void V( ) {
        value = 1;
        notify();
    }
}
