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:
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("Hello World"); //If this isn't included,
//the exit statement isn't executed
for(i=0;i<varint;i++){
System.out.println("Exit");
System.exit(0);
}
}
}
public void run(){
try{
Thread.sleep(5000);
} catch(Exception e){
System.out.println("Caught");
}
varint = 1;
}
}
This is just a small example taken from a much bigger loop. How can I fix this?