title: Why variable is not visible to other thread in synchronization?
body: <p>Let's assume I've two threads <code>t1</code> and <code>t2</code> which are trying to access <code>incX()</code> </p>
  
  <p>Here is my following code: </p>
  
  <pre><code>    class Test implements Runnable {
      private int x  = 0;
  
      public void incX() {
      synchronized(this) {
      x = ++x; 
      }
      System.out.println("x is: "+x+"     "+Thread.currentThread().getName());
     }
  
      public void run() {
      incX();
  }
  
   public static void main(String[] args)   {
  
     Thread t1 = new Thread(new Test());
     t1.start();
     Thread t2 = new Thread(new Test());
     t2.start();
  
  }
  </code></pre>
  
  <p>Here's my output: </p>
  
  <pre><code>    x is: 1     Thread-1
      x is: 1     Thread-0
  </code></pre>
  
  <p>As in <code>incX()</code> method I've synchronized <code>x = ++x</code>, so the changes made to thread <code>t1</code> should be visible to thread <code>t2</code>, right? So my output should be:</p>
  
  <pre><code>    x is: 1     Thread-1
      x is: 2     Thread-0
  </code></pre>
  
  <p>I know <code>++x</code> is not an atomic operation but it is synchronized, so thread <code>t2</code> can't acquire the lock. So the threads should not <code>interleave</code> and changes made to <code>x</code> should be visible to thread <code>t2</code>, right? Am I misunderstanding?
  So my question is why I am not getting the output:</p>
  
  <pre><code>     x is: 2     Thread-0
  </code></pre>