While loop bug? Stuck on empty

If doing this you should make sure the boolean field is also marked volatile. The other thread is effectively allowed to ignore the fact that it’s changed otherwise, particularly a problem in a hard loop! The JIT compiler can end up compiling your code such that the check happens once before your loop. The print is probably changing this behaviour.