class first extends Thread {
  first() {
    System.out.println("Starting first");
    start();
  }
  synchronized public void run() {
    new second(this);
    try {
      wait();
    } catch (InterruptedException e) {}
    System.out.println("Wait ended");
  }
}

class second extends Thread {
  first f;
  second(first F) {
    f = F;
    System.out.println("Starting  second");
    start();
  }
  public void run() {
    System.out.println("entering sleep()");
    try {
      sleep(1500);
    } catch(Exception e) {}
    synchronized(f) {
      f.notifyAll();
    }
  }
}

public class ch14ex4 {
  public static void main(String args[]) {
    new first();
  }
}
