Quick revision
The short answer
A write may be invisible or seen out of order for two kinds of reasons. The JIT compiler may reorder, eliminate or hoist reads and writes that look independent within one thread, and the CPU may reorder memory operations and hold stores in a store buffer before they reach the shared cache, so on weaker architectures other cores can observe writes in a different order from program order. The Java Memory Model allows all of this unless a happens-before edge connects the write and the read, and synchronization constructs such as volatile, locks and the concurrency classes insert the compiler constraints and CPU barriers needed.
What to remember
In this answer, we gave up the picture of a single shared memory updated in program order. The JIT may hoist, remove and reorder accesses that look independent within a thread, cores execute out of order and buffer stores, and on weaker architectures other cores can observe writes to different locations in a different order. The JMM permits all of it unless a happens-before edge connects the write and the read, and volatile, locks and the concurrency classes create those edges by constraining the compiler and inserting the needed barriers.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A write may be invisible or seen out of order for two kinds of reasons. The JIT compiler may reorder, eliminate or hoist reads and writes that look independent within one thread, and the CPU may reorder memory operations and hold stores in a store buffer before they reach the shared cache, so on weaker architectures other cores can observe writes in a different order from program order. The Java Memory Model allows all of this unless a happens-before edge connects the write and the read, and synchronization constructs such as volatile, locks and the concurrency classes insert the compiler constraints and CPU barriers needed.
Why this question is asked
When we write two lines of code, we picture them running in that order, with each write landing in a single shared memory that every thread reads. That picture is fine for one thread and wrong for several. Interviewers ask this question to see whether we know where the surprises come from, because once we understand the causes, the rules of the memory model stop looking arbitrary. In this answer, we will see the simple picture we have to give up, then look at the compiler, the CPU and caches in turn, and finish with how the Java Memory Model and synchronization tie them together.
1The Picture We Have to Give Up
The intuitive model is called sequential consistency: all threads' operations happen one at a time in some interleaving, and each thread's operations appear in program order. If hardware and compilers worked that way, a write would be visible to everyone the moment it happened.
Real systems do not work that way, because sequential consistency is slow. Memory is hundreds of times slower than a CPU register, so every layer between our code and RAM takes shortcuts. Each shortcut is invisible to a single thread, which is the only promise each layer makes. A second thread is not covered by that promise, so it can see the effects of the shortcuts.
2Reason 1: The JIT Compiler Reorders and Removes Accesses
The JIT compiler optimises each thread's code as if no other thread existed, unless synchronization tells it otherwise. It may keep a field in a register instead of reading memory every time, which is called hoisting when it moves a read out of a loop. It may remove a write that is overwritten soon after. And it may reorder independent accesses to schedule instructions better.
A thread that waits for done may never see the change, and a thread that reads y and then x may see y as 2 and x as 0. Neither is a JVM bug; both are legal optimizations for code without synchronization.
3Reason 2: The CPU Reorders and Buffers Memory Operations
Processors also reorder. A modern core executes instructions out of order, starting later loads while earlier ones wait for memory. And each core has a store buffer, a small queue that holds writes so the core does not have to wait for each one to reach the cache. A write sitting in the store buffer is visible to its own core but not yet to the others.
- Source codex = 1
- JIT compiled codemay reorder, merge, or keep in a register
- Core and store bufferwrite queued, visible to this core only
- Private L1 and L2 cachescoherence protocol propagates the line
- Shared cache and memoryother cores can now read the new value
5 layers, from the top down. Step through them one at a time.
The store buffer explains even the strongest common hardware. On x86, a thread can write x = 1 and then read y, and the read can complete while the write is still in the buffer, so two threads doing the mirror image of this can both read 0. Weaker architectures such as ARM go further: they may also let later stores overtake earlier stores and later loads overtake earlier loads.
4Reason 3: Caches and the Order Other Cores Observe
Each core has private caches, and a cache coherence protocol makes sure all cores eventually agree on the value of each memory location. Coherence is about one location at a time. It guarantees that a write to x eventually reaches every core and that all cores agree on the order of writes to x. It does not guarantee that writes to different locations, x and y, become visible to other cores in the order they were made.
So on weaker architectures, one core may see the new y before the new x, while another core sees them the other way around. Program order in the writing thread says nothing about the order another core observes unless something enforces it.
| Source | What it does | Seen by another thread as |
|---|---|---|
| JIT compiler | Hoists, removes or reorders accesses | Stale value forever, or writes out of order |
| Out-of-order core | Starts later loads early | Reads that seem to happen too soon |
| Store buffer | Delays a write's visibility | Old value read after the write |
| Weak memory model (ARM) | Lets writes to different lines appear in any order | Flag seen before the data it guards |
5How the Java Memory Model Ties It Together
Java does not ask us to know any of this per platform. The JMM says that a read is guaranteed to see a write only if the write happens-before the read. Happens-before edges come from program order, unlock before a later lock of the same monitor, a volatile write before a later read of it, thread start and join, and the guarantees documented by java.util.concurrent. Without an edge, all the reorderings above are allowed.
When we do create an edge, the JVM does two things. It stops its own compiler from moving accesses across the synchronization point, and it emits the CPU instructions the platform needs: on x86 a locked instruction after a volatile write to drain the store buffer, on ARM load-acquire and store-release instructions or barriers. The same Java code is then correct on every CPU, which is the point of having the model in the language.
6Follow-up questions
Is the CPU cache the main reason for stale reads?
Not really. Coherence protocols keep caches consistent for each location, so a cache by itself does not keep an old value forever. The more common causes are the compiler keeping a value in a register, the store buffer delaying a write, and weak hardware ordering between different locations. That is why the JMM talks about ordering and happens-before rather than about flushing caches.
Why does code that works on x86 fail on ARM?
x86 only allows a later load to pass an earlier store; it keeps stores in order and loads in order. ARM allows all four reorderings. A program with a data race may happen to be correct on x86 because the hardware never performs the reordering the JMM allows, and then fail on ARM where it does. The program was wrong on both.
Does synchronized flush everything to main memory?
That description is a simplification from older explanations. What synchronized really guarantees is happens-before between an unlock and a later lock of the same monitor. The JVM implements it with whatever compiler restrictions and CPU barriers the platform needs, which is often far cheaper than flushing caches, since coherence already keeps caches in agreement.
7Conclusion
In this answer, we gave up the picture of a single shared memory updated in program order. The JIT may hoist, remove and reorder accesses that look independent within a thread, cores execute out of order and buffer stores, and on weaker architectures other cores can observe writes to different locations in a different order. The JMM permits all of it unless a happens-before edge connects the write and the read, and volatile, locks and the concurrency classes create those edges by constraining the compiler and inserting the needed barriers.
Open with
There are two layers that can make a write invisible or out of order, and the memory model allows both unless there is a happens-before edge. The first is the JIT compiler.
Then, if they want more
It optimises each thread as if it were alone, so it can keep a field in a register and hoist a read out of a loop, remove writes it thinks are redundant, or reorder two writes that look independent. That is how a loop on a plain flag can run forever. The second is the hardware.
Cores execute out of order, and each core has a store buffer, so a write can sit there, visible to its own core but not to others, while later reads go ahead.
Caches are kept coherent per location, but on weakly ordered CPUs like ARM, writes to different locations can become visible to other cores in a different order than they were made, so another thread can see a flag set before the data it guards. x86 is stronger and only allows a later load to pass an earlier store, which is why some racy code works there and fails on ARM.
The Java Memory Model abstracts all this: a read is only guaranteed to see a write if the write happens-before it.
When I use volatile, a lock, thread start and join, or a concurrent collection, the JVM stops the compiler from moving accesses across those points and emits the right barriers for the CPU, so the same code is correct everywhere.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Give the main reasons a write by one thread may not be seen, or may be seen out of...": https://scrivio-interview-library.netlify.app/java/java-memory-model/e30cc70e0b/