public class Semaphore {
    private int value;
    public Semaphore (int initial) {value = initial;}

    synchronized public void P( )
        throws InterruptedException {
        while (value == 0) wait( );
        --value;}

    synchronized public void V( ) {
        ++value;
        notify( );}

}