title: How Java java.util.concurrent.locks.Lock guarantees memory sync between threads?
body: <p>Check out this java code snippet:</p>
  
  <pre><code> Lock l = ReentrantLock();
   l.lock();
   try {
       counter++;
   } finally {
       l.unlock();
   }
  </code></pre>
  
  <p>This code guarantees that only one thread will execute the code in the try block in a particular time.</p>
  
  <p><strong>My question is: how the lock/unlock mechanism guarantees memory visibility between the threads?</strong> (what is the mechanism that will make sure that the counter result will be flushed into the main memory by Thread X, and will be loaded from the main memory by Thread Y that will come afterward?)</p>