title: Java: Multiple threads decrementing static variable via synchronized method accessing variable at the same time
body: <p>I'm trying to synchronize my <code>Person</code> class Methods so that my static counter variable gets decremented by one thread at the a time. </p>
  
  <pre><code>public class Person extends Thread {
  
      private static int count = 10;
  
      public void decrement() {
          synchronized(Person.class) {
              count--;
          }
  
      }
  
      public int getCount() {
          return count;
      }
  
      public void run(){
          while( count &gt; 0){
              this.decrement();
              System.out.print(this.getCount() + ",");
          }
      }
  }
  </code></pre>
  
  <p>Here's my main class. Each thread will decrement to static counter via synchronized method to avoid mutiple thread access to same resource.</p>
  
  <pre><code>public class Main {
  
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
  
          Person p1 = new Person();
          Person p2 = new Person();
          Person p3 = new Person();
          Person p4 = new Person();
          Person p5 = new Person();
  
          p1.start();
          p2.start();
          p3.start();
          p4.start();
          p5.start();
      }
  }
  </code></pre>
  
  <p>But when i run my program, it is printing duplicate counter values. What am i doing wrong?</p>
  
  <p>Outputs: </p>
  
  <pre><code>8,8,7,6,5,4,3,2,1,0
  
  8,7,5,3,1,0,6,8,4,0
  </code></pre>