title: Why Synchronize single statements in Java?
body: <p>I am an analog guy learning how to code on android so please bear with me if this question sounds stupid.</p>
  
  <p>I understand the gist of synchronization. If i have data that two threads can access and modify it could lead to inconsistent values in the data variable in both threads. </p>
  
  <p>It makes sense to make methods synchronized and lock them till one thread is done executing it (performing any conditional checks) but why would it matter with single statements? It would matter with multiple statements as theres a chance of thread one going out of runnable state in between 2 or more statements but i dont understand what the point is for a single statement.</p>
  
  <p>Here is an example</p>
  
  <p>if i have a listener that can be called from lets say a game thread and it modifies a data variable why would i do it this way...</p>
  
  <pre><code>    public void onCompletionListener(MediaPlayer player){
      synchronized (this){
          isPrepared = false;
         }
      }
  </code></pre>
  
  <p>i also have another method in my Music class called stop</p>
  
  <pre><code>   public void stop(){
     mediaPlayer.stop()
      synchronized(this){
      isPrepared = false;
         }
      }
  </code></pre>
  
  <p>Why would i bother to do this? Is this done for consistency of the value between the two threads? if so can i use something like volatile? would that be better?</p>
  
  <p>I ask because i heard that synchronization is expensive. Are there better alternatives? </p>
  
  <p>Thank you all in advance for answering my question. I appreciate it.
  Edit: I read this <a href="https://stackoverflow.com/questions/9669677/synchronization-on-single-statement">synchronization on single statement?</a></p>
  
  <p>I think i understand.. if someone could elaborate that would be awesome. Also part two of my initial question regarding whether volatile is a better alternative is unanswered :)</p>