Scrivio Interview library
ConceptPrincipal7 min read · updated 12 November 2026

Java defined its memory model in the language specification, revised by JSR-133 in Java 5, years before C and C++ standardised theirs in 2011. What did this give the Java ecosystem, and what should engineers in your organisation still be taught about it?

By defining its memory model in the language specification, and fixing it with JSR-133 in Java 5, Java gave every program the same concurrency semantics on every CPU and JIT, so correctly synchronized code behaves the same on x86 and ARM, and libraries such as java.util.concurrent could be specified and implemented portably. JSR-133 repaired the original model's weak volatile and final field guarantees. Engineers should still be taught happens-before, safe publication and that data races give surprising results, while the fine-grained memory reasoning is left to a few expert reviewers and everyone else uses the high-level utilities.

Background to this question

Most engineers never read the Java Memory Model, yet every concurrent Java program relies on it. It is the reason a volatile flag works the same on a laptop and on an ARM server, and the reason ConcurrentHashMap can promise what it promises. A principal engineer is asked about its history because the history explains what the model gives us, which mistakes it was designed to fix, and how much of it each engineer actually needs to know. In this answer, we will see what problem a language-level memory model solves, what went wrong in the original Java model and how JSR-133 fixed it, what this gave the ecosystem, and then what I would teach engineers in an organisation and what I would leave to specialists.

1What a Memory Model Is For

A memory model is the part of a language specification that answers one question: when a thread reads a shared variable, which writes is it allowed to see? Without an answer, the meaning of a concurrent program depends on whatever the compiler and the processor happen to do. Compilers reorder and cache values in registers, and processors buffer stores and execute loads out of order, each in their own way. x86 keeps most memory operations in order; ARM and POWER reorder much more freely.

Before 2011, C and C++ had no memory model in their standards at all. Threads came from libraries such as POSIX threads, and their behaviour was defined partly by those libraries and partly by what each compiler chose to promise. Writing portable concurrent code meant relying on compiler-specific behaviour. Java took the opposite route: because Java threads were part of the language from version 1.0, the language specification had to say what they mean.

2What Was Wrong With the Original Model, and How JSR-133 Fixed It

The first Java memory model, in chapter 17 of the original specification, tried to define this, but it had serious problems. It was hard to understand, even for experts. It forbade some compiler optimizations that every JVM performed anyway, so real JVMs did not follow it. And its guarantees were too weak in the places programmers relied on most.

Two weaknesses mattered most. Volatile was too weak: a volatile write was not guaranteed to make earlier ordinary writes visible, so the common pattern of filling an object and then setting a volatile flag was not correct. Final fields had no special guarantee either: a thread could see a String's reference and still see its characters as not yet set, so even immutable objects could appear to change. This broke the security assumption that a String cannot change after it has been checked.

JSR-133, a Java Community Process effort led by researchers and JVM engineers, rewrote the model for Java 5 in 2004. It defined everything through the happens-before relation, strengthened volatile so that a volatile write and a later read of it order and publish the surrounding writes, gave final fields a guarantee of visibility after a properly finished constructor, and promised that correctly synchronized programs, those with no data races, behave as if threads were simply interleaved.

Before and after JSR-133
AreaOriginal model (to Java 1.4)JSR-133 (Java 5 and later)
VolatileOrdered only volatile accessesWrite publishes all earlier writes to readers
Final fieldsNo special visibilityVisible after the constructor ends
Double-checked lockingBroken in every formCorrect with a volatile field
Racy programsVague, hard to implementDefined, with limits on surprises
Race-free programsUnclearSequentially consistent

3What This Gave the Java Ecosystem

The biggest gift was portability of concurrency semantics. A Java program that is correctly synchronized has the same meaning on x86, ARM, POWER and any future CPU, under any conforming JIT. When a company moves its services from x86 to ARM cloud instances today, the code that follows the rules keeps working without changes, because the JVM, not the programmer, is responsible for emitting the right barriers on each architecture.

The second gift was java.util.concurrent, also delivered in Java 5. Classes such as ConcurrentHashMap, ReentrantLock, the atomic classes and the executors could be specified with precise guarantees, the memory consistency effects in their Javadoc, and implemented once in portable Java on top of volatile and compare-and-set. Without a defined model, each of those classes would have needed per-platform reasoning or native code. Java's model later also influenced the C++11 memory model, which took some of the same ideas, such as defining races and guaranteeing sequential consistency for race-free programs.

4What Every Engineer Should Be Taught

Not every engineer needs to read chapter 17 of the specification, but every engineer who writes code that runs on more than one thread needs a short, correct mental model. I would teach three ideas.

First, happens-before: a write in one thread is guaranteed to be visible in another only if a happens-before edge connects them, and the edges come from locks, volatile, thread start and join, and the concurrency classes. Second, safe publication: sharing an object means making both the reference and its contents visible, which we do through a final field, a volatile field, a lock, a static initializer or a concurrent collection. Third, data races give surprising results: a loop that never ends, a value that seems to go backwards, an object that looks half built. These are allowed behaviours of a racy program, not JVM bugs, and they often appear only under load or on a different CPU.

With those three ideas, an engineer can recognise a risky pattern in review and knows to reach for the right tool.

5What to Leave to a Few Specialists

The fine-grained parts of the model, such as reasoning about VarHandle acquire and release modes, lock-free algorithms, benign races and the exact guarantees for racy programs, are hard even for experts, and mistakes in them are invisible in normal testing. In an organisation, I would make most engineers use the high-level utilities, immutable objects and existing libraries, and route any code that relies on fine-grained memory reasoning to a small group of reviewers who are trained for it and who require jcstress tests on both x86 and ARM.

Step throughWho works at which level
  1. Application codeimmutable objects, executors, concurrent collections, CompletableFuture
  2. Shared team codelocks, volatile flags, atomics, documented guarding
  3. Reviewed platform librariesVarHandle modes, lock-free structures, jcstress required
  4. JVM and JDKbarriers per CPU, implementation of the model

4 layers, from the top down. Step through them one at a time.

Most engineers stay in the top layer; a few reviewers own the third.

6Follow-up questions

Did JSR-133 make racy programs safe?

No. It made them defined, with some limits such as forbidding values that appear out of thin air, but a racy program can still see stale values and reordered writes. What JSR-133 guarantees is that programs without data races behave as if the threads were interleaved in program order. Safety comes from removing races, not from the model tolerating them.

Why is double-checked locking linked with JSR-133?

Under the original model, no form of double-checked locking was correct, because even a volatile field did not publish the constructor's writes. After JSR-133, declaring the instance field volatile makes the pattern correct, because the volatile write happens-before the read that sees the reference, and the constructor's writes come before that volatile write in program order.

How would you check that teams follow these ideas in practice?

Through review checklists that ask how each shared field is guarded, through static analysis for common patterns such as unsynchronized lazy initialisation, through ArchUnit or similar rules for banned primitives, and through jcstress tests for shared libraries. Running CI on ARM as well as x86 catches the races that x86 hides.

7Conclusion

In this answer, we saw that defining the memory model in the language gave Java portable concurrency semantics years before C and C++, so correctly synchronized code behaves the same on every CPU, and it made java.util.concurrent possible to specify and implement portably. JSR-133 in Java 5 fixed the original model's weak volatile and final field guarantees and rebuilt it on happens-before. In an organisation, every engineer should understand happens-before, safe publication and the surprises of data races, while the fine-grained reasoning stays with a few reviewers and everyone else uses the high-level utilities.

What to say in the interviewAbout 90 seconds aloud

Open with

The main thing Java gained by putting the memory model in the language specification was portable concurrency semantics. A correctly synchronized Java program means the same thing on x86, ARM or any other CPU, under any conforming JIT, because the JVM is responsible for emitting the right barriers.

Then, if they want more

C and C++ relied on platform and compiler behaviour until 2011. That portability is why companies can move Java services to ARM today without rewriting them, as long as the code follows the rules. It also made java.util.concurrent possible: ConcurrentHashMap, the locks and atomics and executors could be specified with precise memory guarantees and implemented once, portably, in Java.

The original model had real flaws, and JSR-133 in Java 5 fixed them. Volatile was too weak to publish other writes, so filling an object and then setting a volatile flag was not safe, and final fields had no visibility guarantee, so even Strings could appear to change.

JSR-133 rebuilt the model on happens-before, strengthened volatile, gave final fields their guarantee, and promised sequential consistency for race-free programs. For my organisation, I would teach every engineer three ideas: happens-before, safe publication, and that data races cause surprising results that are not JVM bugs.

Then I would steer most engineers to immutable objects and the high-level utilities, and keep fine-grained reasoning, like VarHandle modes and lock-free code, with a small group of reviewers who require jcstress tests on x86 and ARM.

About this answer

Last updated
12 November 2026
Versions it names
Java 5
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 "Java defined its memory model in the language specification, revised by JSR-133 in Java...": https://scrivio-interview-library.netlify.app/java/java-memory-model/09a0908b4d/