public class Semaphore {
    int n;
    String name;

    public Semaphore(int max) {
	n = max;
    }

    public synchronized void P() {
	if (n == 0) {
	    try {
		wait();
	    } catch(InterruptedException ex) {};
	}
	n--;
    }
    
    public synchronized void V() {
	n++;
	notify();
    }
}
