Concurrency
The Java Memory Model in Practice
Fifteen billion observations on AArch64 produced 191,614 the model permits and x86 forbids. One in 79,000 - too rare to test for, common enough to ship.

On this page
Fifteen billion observations, and 191,614 of them saw a flag set with the data it was announcing still unwritten.
That is one in 79,079 — a rate low enough that a test suite exercising the path a thousand times will pass every run for years, and high enough that a service doing it ten thousand times a second meets it about every eight seconds. The Java memory model permits the outcome. This hardware produces it. Nothing in between reports it.
The run: JDK 26.0.2.1 (Homebrew build), macOS 26.6.2, Apple M4 Pro, with os.arch reporting aarch64, twenty seconds per configuration.
The shape is the textbook one. A writer stores a value and then sets a flag; a reader loads the flag and, if it is set, loads the value:
// writer // reader
data[k] = 42; if (flag[k]) {
flag[k] = true; observe(data[k]); // may be 0
}
The first harness saw nothing
The obvious version of that experiment reported no anomalies at all: 59,313,687 observations, zero.
Two things in it were wrong, and both are worth stating because they are what most hand-written concurrency tests do. The reader spun on the flag until it became true, which is a wait rather than an observation and imposes precisely the ordering under test. And data and flag were fields of one object, so they shared a cache line and became visible together. The experiment was well formed as Java and measured nothing.
The second harness had the bug it was looking for
The corrected version put the two locations in separate arrays with a 128-byte stride and stopped spinning. It reported 307,346,070 anomalies in 14,409,543,105 observations — 2.13 per cent.
Every one of them was false. The writer replaced both arrays between generations through two separate volatile fields, and the reader read those two fields separately. Nothing orders one read against the other, so the reader could pair a freshly allocated data array with the previous generation’s flag array: flag set, data zero, and no reordering involved at all.
The instrument had the same class of defect as the code it was built to examine, which is the most direct available argument that reading this kind of code carefully is not optional.
The fix is the final-field guarantee
Both arrays now reach the reader through one holder whose fields are final, published by a single volatile write. That is not a trick; it is the guarantee the specification offers for exactly this:
A thread that can only see a reference to an object after that object has been completely initialized is guaranteed to see the correctly initialized values for that object’s
finalfields.
One volatile write publishes the holder, and the freeze on its final fields carries both arrays with it. The reader’s single read of the holder is then a consistent pair by construction, and any anomaly it counts afterwards is an ordering observation rather than a mismatched generation.
What the model permits, and what the machine delivers
With the harness correct, the plain version produced 191,614 anomalies in 15,152,744,634 observations: 0.001265 per cent.
The reordering is legal. There is no happens-before edge between the writer’s store to data and the reader’s load of it, because the only candidate — the flag — is an ordinary field. The specification’s edge requires a volatile: “A write to a volatile field happens-before every subsequent read of that field.” Absent that, the two threads have no ordering relationship and the JVM and the hardware are both free to arrange the stores as they like.
The hardware is what turns legal into observed. Intel’s model states that “neither loads nor stores are reordered with like operations”, so this store-store case does not arise there. AArch64 makes no equivalent promise. The same source, the same JDK, and a failure rate that is zero on one machine and one in 79,000 on another — which is the argument for programming against the model rather than against the machine in front of you, in a portable language whose deployment target you do not choose.
Release and acquire remove it entirely
Replacing the two plain accesses with setRelease on the write and getAcquire on the read, changing nothing else, produced 0 anomalies in 15,339,059,047 observations.
Not fewer. None. The pairing establishes the edge the plain version lacked, and once the edge exists the outcome is not merely unlikely but forbidden. A volatile field would do the same thing with a stronger guarantee than this case needs, which is the distinction the access modes exist to express.
Happens-before as a reading tool
The practical use of the model is not to predict what will happen. It is to read a publication site and answer one question: what edge orders the write against the read? If the answer is a volatile write followed by a read of that field, a final field seen through a properly published reference, a lock, or a release paired with an acquire, the code is ordered. If the answer is “it has worked so far”, the code is unordered and the rate is a property of hardware nobody has committed to.
That reading is cheap and it is the only method that scales, because the alternative — testing — was measured here at one in 79,079 on the machine that produces the failure most readily. A thread dump will not show it, and unlike false sharing, which costs throughput and shows up in a benchmark’s error bars, this one costs correctness and shows up as a bug report nobody can reproduce.
Frequently asked
- If my concurrency test passes, is the publication safe?
- A passing test is weak evidence at this rate. The failure measured here appeared once in 79,079 observations, so a suite that exercises the path a thousand times will pass for years while the code remains wrong. The reasoning has to come from the happens-before edges, not from the test result.
- Does this happen on x86 too?
- Not this particular reordering. Intel's memory-ordering model states that neither loads nor stores are reordered with like operations, so the store-store case measured here is forbidden there. AArch64 makes no such promise, which is why the same code has different failure rates on different machines and why the model, not the hardware, is what to program against.


