title: Java synchronized block and the visibility guarantees for an explicit lock
body: <p>I have a class, in which I have a state that consists of several fields-collections.</p>
  <p>Now, I have methods that change that state. And there are methods that are reading it.</p>
  <p>I want to use an explicit lock to guard state change modifications.</p>
  <pre><code>class Sample {
    private final Map&lt;String&gt; a = new HashMap&lt;&gt;();
    private final Map&lt;String&gt; b = new HashMap&lt;&gt;();
    private final Map&lt;String&gt; c = new HashMap&lt;&gt;();
  
    // protects the state of variables a,b,c so that the state modifications are atomic
    private final Object lock = new Object();
  
    ...
    // other fields
  
    private void changeState(StateChange obj){
       synchronized(lock){
           changeA();
           changeB();
           changeC();
      }
    }
  
    private State readState(){
       synchronized(lock){
           return buildStateFromABC();
      }
    }
  
  }
  </code></pre>
  <p>If this class is accessed/modified in a multi-threaded environment, will each thread see up-to-date contents of fields a,b,c after entering a guarded area?</p>
  <p>There is no direct correlation between the lock and those fields, or is there something?</p>
  <p>I know that I can use an intrinsic lock here and just slap synchronized on methods, but I want to minimize the protected area.</p>
  <p>What would be the preferred way of synchronizing here? On explicit lock object or on <code>this</code>?</p>