title: Java: For loop in while loop not running unless println statement included
body: <p>I am trying to run a for loop inside an infinite while loop. The code doesn't run as intended when there's no print statement, but when there is a print statement, it runs fine. Here is the code:</p>
  <pre><code>class Test implements Runnable{
    public static int varint = 0;
    public static void main(String args[]){
      Thread x = new Thread(new Test());
      int i;
      x.start();
      while(true){
        System.out.println(&quot;Hello World&quot;); //If this isn't included, 
                                          //the exit statement isn't executed
        for(i=0;i&lt;varint;i++){
          System.out.println(&quot;Exit&quot;);
          System.exit(0);
        }
      }
    }
  
    public void run(){
      try{
        Thread.sleep(5000);
      } catch(Exception e){
        System.out.println(&quot;Caught&quot;);
      }
      varint = 1;
    }
  }
  </code></pre>
  <p>This is just a small example taken from a much bigger loop. How can I fix this?</p>