title: Java concurrency. Block application from exiting
body: <p>I am trying to synchronize different threads in my application. Basically there are some threads that should be able to block the application from closing. Here is what I have:</p>
  
  <pre><code>public class Test{
    public static void main(String args[]){
      Syncer syncer = new Syncer();
      Object o = new Object();
      syncer.lockExit(o);
      //.. do some stuff//
      syncer.unlockExit(o);
    }
  }
  
  public class Syncer {
  
      private List&lt;Object&gt; l_Exit;
  
      public Syncer() {
          l_Exit = new ArrayList&lt;Object&gt;();
      }
  
      public synchronized void lockExit(Object o) {
          l_Exit.add(o);
      }
  
      public synchronized void unlockExit(Object o) {
          l_Exit.remove(o);
          if (l_Exit.isEmpty()) {
              l_Exit.notifyAll();
          }
      }
  
      public synchronized void waitForExit() {
          while (!l_Exit.isEmpty()) {
              try {
                  l_Exit.wait();
              } catch (InterruptedException ex) {
                  Test.log.log(Level.SEVERE, null, ex);
              }
          }
      }
  }
  </code></pre>
  
  <p>I get IllegalMonitorException when running syncer.unlockExit(o);</p>