title: Code to simulate race condition in Java thread
body: <p>I am new to Java multithreading.  I am learning the concept of race condition.</p>
  
  <p>Based on the Oracle document</p>
  
  <p><a href="http://docs.oracle.com/javase/tutorial/essential/concurrency/interfere.html" rel="noreferrer">http://docs.oracle.com/javase/tutorial/essential/concurrency/interfere.html</a></p>
  
  <p>I created a sample code like the one below</p>
  
  <pre><code>public class CounterTest {
  
      public static void main(String[] args) {
  
          Thread thread1 = new Thread(new CounterIncThread());
          thread1.setName("add thread");
          thread1.start();
  
          Thread thread2 = new Thread(new CounterDecThread());
          thread2.setName("sub thread");
          thread2.start();
  
          Thread thread3 = new Thread(new CounterIncThread());
          thread3.setName("add thread2");
          thread3.start();
      }
  
  }
  
  
  class CounterIncThread implements Runnable {
      public void run() {
          SynchronizedCounter counter = new SynchronizedCounter();
          counter.increment();
          try {
              Thread.sleep(4000);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
          String threadName =
                  Thread.currentThread().getName();
          System.out.println(threadName+ ": "+counter.value());
      }
  }
  
  class CounterDecThread implements Runnable {
      public void run() {
          SynchronizedCounter counter = new SynchronizedCounter();
          counter.decrement();
          String threadName =
                  Thread.currentThread().getName();
          System.out.println(threadName+ ": "+counter.value());
      }
  }
  
  class SynchronizedCounter {
      private int c = 0;
  
      public  void increment() {
          c++;
      }
  
      public   void decrement() {
          c--;
      }
  
      public  int value() {
          return c;
      }
  
  }
  </code></pre>
  
  <p>The code doesn't show any racing condition.  Can you please help me, how to stimulate the race condition using the above code?</p>
  
  <p>Thanks</p>