title: Difference between volatile and synchronized in Java
body: <p>I am wondering at the difference between declaring a variable as <code>volatile</code> and always accessing the variable in a <code>synchronized(this)</code> block in Java?</p>
  
  <p>According to this article <a href="http://www.javamex.com/tutorials/synchronization_volatile.shtml" rel="noreferrer">http://www.javamex.com/tutorials/synchronization_volatile.shtml</a> there is a lot to be said and there are many differences but also some similarities.</p>
  
  <p>I am particularly interested in this piece of info:</p>
  
  <blockquote>
    <p>...</p>
    
    <ul>
    <li>access to a volatile variable never has the potential to block: we're only ever doing a simple read or write, so unlike a synchronized block we will never hold on to any lock;</li>
    <li>because accessing a volatile variable never holds a lock, it is not suitable for cases where we want to <strong>read-update-write</strong> as an atomic operation (unless we're prepared to "miss an update");</li>
    </ul>
  </blockquote>
  
  <p>What do they mean by <strong>read-update-write</strong>? Isn't a write also an update or do they simply mean that the <strong>update</strong> is a write that depends on the read?</p>
  
  <p>Most of all, when is it more suitable to declare variables <code>volatile</code> rather than access them through a <code>synchronized</code> block? Is it a good idea to use <code>volatile</code> for variables that depend on input? For instance, there is a variable called <code>render</code> that is read through the rendering loop and set by a keypress event?</p>