title: java.lang.IllegalMonitorStateException: (m=null) Failed to get monitor for
body: <p>Why may this happen? The thing is that monitor object is not null for sure, but still we get this exception quite often:</p>
  
  <pre><code>java.lang.IllegalMonitorStateException: (m=null) Failed to get monitor for (tIdx=60)
          at java.lang.Object.wait(Object.java:474)
          at ...
  </code></pre>
  
  <p>The code that provokes this is a simple pool solution:</p>
  
  <pre><code>    public Object takeObject() {
          Object obj = internalTakeObject();
          while (obj == null) {
              try {
                  available.wait();
              } catch (InterruptedException e) {
                  throw new RuntimeException(e);
              }
              obj = internalTakeObject();
          }
          return obj;
      }
  
      private Object internalTakeObject() {
          Object obj = null;
          synchronized (available) {
              if (available.size() &gt; 0) {
                  obj = available.keySet().iterator().next();
                  available.remove(obj);
                  synchronized (taken) {
                      taken.put(obj, Boolean.valueOf(true));
                  }
              }
          }
          return obj;
      }
  
      public void returnObject(Object obj) {
          synchronized (taken) {
              taken.remove(obj);
          }
          synchronized (available) {
              if (available.size() &lt; size) {
                  available.put(obj, Boolean.valueOf(true));
                  available.notify();
              }
          }
      }
  </code></pre>
  
  <p>Am I missing something?</p>
  
  <p><strong>EDIT</strong>: The exception happens in <code>available.wait();</code> line.</p>