question about “Java Concurrency in Practice” example

Because ready isn’t marked as volatile and the value may be cached at the start of the while loop because it isn’t changed within the while loop. It’s one of the ways the jitter optimizes the code.

So it’s possible that the thread starts before ready = true and reads ready = false caches that thread-locally and never reads it again.

Check out the volatile keyword.

Leave a Comment