title: Synchronized method does not work as expected
body: <p>I have a variable which is shared by two threads. The two threads will do some operations on it. I don't know why the result of sharedVar is different every time I execute the program.</p>
  
  <pre><code>public class Main
  {
      public static int sharedVar = 0;
      public static void main(String[] args) 
      {
          MyThread mt1 = new MyThread();
          MyThread mt2 = new MyThread();
          mt1.start();
          mt2.start();
  
          try
          {
              // wait for the threads
              mt1.join();
              mt2.join();
          }
          catch (InterruptedException e1)
          {
              e1.printStackTrace();
          }
  
          System.out.println(sharedInt); // I expect this value to be 20000, but it's not
      }
  }
  </code></pre>
  
  <p>The following is the class "MyThread"</p>
  
  <pre><code>public class MyThread extends Thread
  {
      private int times = 10000;
      private synchronized void addOne()
      {
          for (int i = 0; i &lt; times; ++i)
          {
              Main.sharedVar ++;
          }
      }
  
      @Override
      public void run()
      {
          addOne();
      }
  }
  </code></pre>
  
  <p>The final result of sharedVar sometimes are 13735, 12508, or 18793; but never 20000, which is the result I expect. Another interesting thing about the program is when times=1000. I always get 2000 as the final result.</p>
  
  <p>Can anyone explain this phenomenon?</p>