public class Semaphore {
  private int init;
  private int s;
  private int del;

  public Semaphore(int i) {
    init = i;
  }

  public synchronized void P() {
    while (s <= 0) {
      del++;
      try {
	wait();
      } catch (InterruptedException e) {}
      del--;
    }
    s--;
  }

  public synchronized void V() {
    s++;
    if (del > 0) {
      notify();
    }
  }
}
