This is because your boolean
is not volatile
, therefore Thread
s are allowed to cache copies of it and never update them. I would recommend an AtomicBoolean
- that will prevent any issues you may have.
public static void main(String args[]) {
final AtomicBoolean done = new AtomicBoolean(false);
new Thread() {
public void run() {
done.set(true);
}
}.start();
while (!done.get());
System.out.println("bye");
}