title: Java Wait and Notify: IllegalMonitorStateException
body: <p>I don't completely understand how <code>wait</code> and <code>notify</code> (of <code>Object</code>) work, and as a result I'm forced to slim down my attempts into the following section of code.</p>
  
  <p>Main.java:</p>
  
  <pre><code>import java.util.ArrayList;
  
  class Main
  {
    public static Main main = null;
  
    public static int numRunners = 4;
    public static ArrayList&lt;Runner&gt; runners = null;
  
    public static void main(String[] args)
    {
      main = new Main();
    }
  
    Main()
    {
      runners = new ArrayList&lt;Runner&gt;(numRunners);
  
      for (int i = 0; i &lt; numRunners; i++)
      {
        Runner r = new Runner();
        runners.add(r);
        new Thread(r).start();
      }
  
      System.out.println("Runners ready.");
      notifyAll();
    }
  }
  </code></pre>
  
  <p>Runner.java:</p>
  
  <pre><code>class Runner implements Runnable
  {
    public void run()
    {
      try
      {
        Main.main.wait();
      } catch (InterruptedException e) {}
      System.out.println("Runner away!");
    }
  }
  </code></pre>
  
  <p>Currently I get an IllegalMonitorStateException when calling <code>Main.main.wait();</code>, but I don't understand why.  From what I can see, I need to synchronize <code>Runner.run</code>, but in doing so I assume it would only notify one thread, when the idea is to notify them all.</p>
  
  <p>I've looked at <code>java.util.concurrent</code>, but I can't find a suitable replacement (maybe I'm just missing something).</p>