public class Garden {
  public static void main(String[] args) throws InterruptedException {
    Counter c = new Counter();
    Thread tWest = new Thread(new Entry(c,"west"));
    Thread tEast = new Thread(new Entry(c,"east"));
    Thread tNorth = new Thread(new Entry(c,"north"));
    Thread tSouth = new Thread(new Entry(c,"south"));
    tWest.start();
    tEast.start();
    tNorth.start();
    tSouth.start();
    tWest.join(0);
    tEast.join(0);
    tNorth.join(0);
    tSouth.join(0);
    System.out.println(c.getValue());
  }
}
class Entry implements Runnable {
  private Counter c;
  private String id;
  public Entry(Counter c, String id) {
    this.c = c;
    this.id = id;
  }
  public void run() {
    for (int i = 0; i < 100; i++) {
      try {
        Thread.sleep((long)(Math.random()*10));
      } catch (InterruptedException e) {}
      this.c.increment();
      System.out.println("Someone arrived at "+this.id+"!");
    }
  }
}
class Counter {
  private int n = 0;
  public synchronized void increment() {
    this.n++;
  }
  public int getValue() {
    return this.n;
  }
}
