title: How to fix IllegalMonitorStateException when using wait and notify?
body: <p>I have a JPanel class which starts another thread using "implements runnable". This other thread will then at various points call a method in the JPanel class which on doing so will then need to wait for the user input. I have attempted to implement it like this:</p>
  
  <p>Method in JPanel class called by other thread that needs to wait:</p>
  
  <pre><code>public void methodToWait()
  {
      while(conditionIsMet)
      {
          try
          {
              wait();
          }
          catch
          {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Method in JPanel class that notifies on wait on user input:</p>
  
  <pre><code>public void mouseClicked(MouseEvent event)
  {
      notifyAll();
  }
  </code></pre>
  
  <p>However, upon running the application it throws a "java.lang.IllegalMonitorStateException" upon calling the wait, why is it doing this and how do I resolve the issue?</p>