I have searched a lot but didn't find solution of this problem so I asked the question here.
I made a small chunk of code to replicate the problem. so following are those Java classes.
The class with the main function:
package test;
public class Main {
public static void main(String[] args) {
Background ob = new Background();
while(ob.val > 0);
System.out.println("Program Completed");
}
}
The runnable class:
package test;
public class Background implements Runnable {
int val;
Thread t;
public Background() {
val = 500;
t=new Thread(this);
t.start();
}
@Override
public void run() {
while(val > -1000) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
val--;
}
}
}
Now this code is not printing "Program Completed", so I'm thinking that the while loop is in infinite loop.
But if I replace while(ob.val > 0);
with
while(ob.val > 0){
System.out.println("val: "+ob.val);
};
or any System.out.println()
statement then I can see "Program Completed".
But yes only any System.out.println()
statement.
If I replace while(ob.val > 0);
with
int g;
while(ob.val > 0){
g = 0;
};
Above code also not showing "Program Completed".
The main code is too large to post here so I have replicated the problem. Tested th replicated code in ubuntu 14.04, JDK -> jdk-8u31-linux-x64
I didnt tested this replicated code in Windows. But I tested the main code in windows & its working fine.
I'm really confused with this type of behavior. Will any one help me?? Thanks in advance.