title: Increment an int using threads
body: <p>I have two threads and I want to increment an int variable while it is less than a certain value. My program does that but two different threads do not increment the value separately.<br>
  If <code>one</code> and <code>two</code> are two threads, I want the output to be like this  </p>
  
  <pre><code>one 1  
  two 2  
  one 3  
  two 4  
  one 5  
  two 6  
  one 7  
  two 8...  
  </code></pre>
  
  <p>Here is my program:  </p>
  
  <pre><code>class IncTo100Demo implements Runnable {
      public volatile int count = 0;
  
      public void run() {
          Thread current = Thread.currentThread();
          try {
              while(count &lt; 21) {
                  System.out.println(current.getName() + " count = " + count++);
                  current.sleep(1000);
              }
          }
          catch(Exception e) {
              e.printStackTrace();
          }
      }
  
      public static void main(String[] args) {
          Thread one = new Thread(new IncTo100Demo(), "one");
          Thread two = new Thread(new IncTo100Demo(), "two");
  
          try {
              one.start();
              two.start();
              System.out.println("one alive? " + one.isAlive());
              one.join();
          }
          catch(Exception e) {
              e.printStackTrace();
          }
      }
  }  
  </code></pre>
  
  <p>and its output is:  </p>
  
  <pre><code>one alive? true
  two count = 0
  one count = 0
  two count = 1
  one count = 1
  two count = 2
  one count = 2
  two count = 3
  one count = 3
  two count = 4
  one count = 4
  two count = 5
  one count = 5
  two count = 6
  one count = 6
  two count = 7
  one count = 7
  two count = 8
  one count = 8
  two count = 9  
  </code></pre>
  
  <p>How can I make these threads update the variable individually so that the output can look like what I want it to?</p>