title: Race condition: Min and Max range of an integer
body: <p>I was recently asked this question in an interview.</p>
  
  <p>Given the following code, what will be the min and max possible value of the static integer <code>num</code>?</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  public class ThreadTest {
      private static int num = 0;
  
      public static void foo() {
          for (int i = 0; i &lt; 5; i++) {
              num++;
          }
      }
  
      public static void main(String[] args) throws Exception{
          List&lt;Thread&gt; threads = new ArrayList&lt;Thread&gt;();
          for (int i = 0; i &lt; 5; i++) {
              Thread thread = new Thread(new Task());
              threads.add(thread);
              thread.start();
          }
          for (int i = 0; i &lt; 5; i++) {
              threads.get(i).join();
          }
          // What will be the range of num ???
          System.out.println(ThreadTest.num);
      }
  }
  
  class Task implements Runnable {
      @Override
      public void run() {
          ThreadTest.foo();
      }
  
  }
  </code></pre>
  
  <p>I told them that the max value would be 25 (in case there is no race condition), and min would be 5 (in case of a race condition between all the threads at every iteration).<br>
  But the interviewer said the the min value can go even below 5.<br>
  How is that possible?</p>