title: java concurrency synchronized on map value
body: <p>The following code, I am confused about what would happen when 2 threads compete the lock for <code>map.get(k)</code>. When thread A wins, it makes <code>map.get(k)</code> null and the second thread would get a <code>synchronized(null)</code>? Or would it be both threads see it as <code>synchronized(v)</code> even though the first thread changes it to null but during which thread B still sees it as <code>v</code>?</p>
  
  <pre><code>synchronized(map.get(k)) {
     map.get(k).notify();
     map.remove(k);
  }
  </code></pre>
  
  <p>The question is a similar to <a href="https://stackoverflow.com/questions/10195054/synchronized-object-set-to-null">another question</a>, except lock object is value of a map.</p>
  
  <p><strong>UPDATE</strong>:
  compared the discussion in this post and that in the above link, is it true that</p>
  
  <pre><code>synchronized(v) {
      v.notify();
      v = null;
  } 
  </code></pre>
  
  <p>would cause the 2nd thread <code>synchronized(null)</code>. But for the <code>synchronized(map.get(k))</code>, the 2nd thread would have <code>synchronized(v)</code>??? </p>
  
  <p><strong>UPDATE</strong>:
  To answer @Holger's question, the main difference between this post and the other one is:</p>
  
  <pre><code>final V v = new V();
  synchonized(map.get(k)) {
      map.get(k).notify();
      map.remove(k);
  }
  </code></pre>