title: How does multiple threads acquire and release lock in synchronized methods and synchronized blocks in java?
body: <p>How does the java thread acquire a lock on a monitor used in synchronized block or monitor used in synchronized methods?</p>
  <p>I read on multiple posts that in case of biased locking this information is stored in the object header using CAS operation and in case of contended situation wait set queue/ monitor queue is used but eventually lock marked in the object header only.
  If this is the case then how is lock released? How object is marked free for acquiring a lock by another thread? are wait and notify methods used internally for this? If this is the case then why is making monitor null inside the synchronized block does not throw any exception.</p>
  <p>The below example works perfectly fine, I was expecting NullPointerException assuming the end of the synchronized block will try to mark lock property to free the lock.</p>
  <p>Example:</p>
  <pre><code>Object monitor = new Object();
      synchronized (monitor){
          System.out.println(&quot;before null&quot;);
          monitor =null;
          System.out.println(&quot;after null&quot;);
      }
  
      System.out.println(&quot;successfully Exited&quot;);
  </code></pre>