title: Want a code for the difference between synchronized and asynchronized method
body: <p>I am trying to set the difference between synchronized and unsynchronized methods.. I have tried following code </p>
  
  <pre><code> class Counter {
      private int counter = 10;
      public int getCounter() {
          return counter;
      }
  
      public synchronized void doIncrementAndDecrement() {
          counter++;
          keepBusy(500);
          counter--;
      }
  
      public void keepBusy(int howLong) { // (D)
          long curr = System.currentTimeMillis();
          while (System.currentTimeMillis() &lt; curr + howLong)
              ;
      }
  }
  
  class MyCounterThread extends Thread {
  
      Counter c;
  
      public MyCounterThread(Counter c, String name) {
          // TODO Auto-generated constructor stub
          super(name);
          this.c = c;
          start();
      }
  
      @Override
      public void run() {
          for (;;) {
              c.doIncrementAndDecrement();
              sleepForSometime();
              System.out.println(c.getCounter());
          }
      }
  
      public void sleepForSometime() { // (D)
          try {
              Thread.sleep(1000);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
  }
  
  public class UnSynchronizedExapmle {
  
      public static void main(String[] args) throws InterruptedException {
          Counter c = new Counter();
          MyCounterThread t1 = new MyCounterThread(c, "A");
          MyCounterThread t2 = new MyCounterThread(c, "B");
          MyCounterThread t3 = new MyCounterThread(c, "C");
      }
  }
  </code></pre>
  
  <p>So above i have <strong>doIncrementAndDecrement()</strong> synchronized method.. </p>
  
  <p>So i expected the value of counter should be 10 every time. But this wont happen i have the output is like</p>
  
  <pre><code>10
  10
  11
  10
  10
  10
  10
  11
  10
  10
  11
  10
  11
  11
  10
  10
  11
  10
  11
  10
  10
  10
  11
  10
  10
  11
  10
  </code></pre>
  
  <p>So please help me why this happens.. Or any blog/article for explaining difference between synchronized and asynchronized methods</p>