Loop doesn’t see value changed by other thread without a print statement

The JVM is allowed to assume that other threads do not change the pizzaArrived variable during the loop. In other words, it can hoist the pizzaArrived == false test outside the loop, optimizing this: while (pizzaArrived == false) {} into this: if (pizzaArrived == false) while (true) {} which is an infinite loop. To ensure … Read more