How to demonstrate Java instruction reordering problems?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Java allows compilers, the JVM, and the CPU to reorder operations when single-threaded behavior is preserved. In multithreaded code without a proper happens-before relationship, that freedom can expose results that look impossible, which is why instruction reordering is usually demonstrated with a small shared-memory test.
A Classic Reordering Demonstration
The usual example has two threads writing one variable and reading the other. If no synchronization is present, the program may eventually observe both reads as zero even though each thread performed a write first in source order.
This program is valid Java, but the outcome is not guaranteed to appear quickly. You may need many iterations, and on some machines it can take a long time. That does not make the example wrong; it only means the scheduler and hardware timing have to align before the weakly ordered behavior becomes visible.
Why the Result Is Allowed
Each thread looks simple in isolation. Thread one writes a = 1 and then reads b. Thread two writes b = 1 and then reads a. Without volatile, synchronized, or another memory-ordering construct, there is no rule forcing one thread to observe the other thread's write before doing its own read.
That means the system can legally produce this effect:
- thread one reads the old value of
b - thread two reads the old value of
a
The surprising part is not only compiler reordering. Cache visibility, store buffers, and general lack of ordering also contribute. In practice, people often say "instruction reordering" as a shorthand for the whole category of memory-ordering problems.
Making the Example Deterministic Enough to Study
If you want to make the race easier to trigger, align the thread starts more closely. A barrier reduces the chance that one thread finishes most of its work before the other even begins.
This still does not guarantee the 0, 0 observation, but it makes the demonstration more practical.
Fixing the Problem
The correct lesson is not "avoid thread scheduling." The lesson is to establish a happens-before relationship. One straightforward fix is to mark the shared variables as volatile or guard the critical section with synchronized.
Once ordering is explicit, the weird result is no longer allowed by the Java Memory Model.
Common Pitfalls
- Expecting the reordering outcome to appear immediately on every machine.
- Confusing lack of visibility with only compiler optimization.
- Using a demo without synchronization and then assuming the outcome proves a JVM bug.
- Fixing the sample with sleeps instead of proper memory-ordering primitives.
- Forgetting that
joinonly orders the main thread after worker completion, not the worker operations relative to each other.
Summary
- Java can expose surprising shared-memory results when code has a data race.
- The classic two-thread litmus test shows how both reads can observe stale values.
- The effect comes from missing ordering guarantees, not from one single optimization stage.
- A barrier can make the demo easier to observe, but it does not fix the race.
- Use
volatile,synchronized, or higher-level concurrency tools to make ordering explicit.
Related reading
- How to detect and debug multi-threading problems?
- How to detect stdin input in a cancel-safe way in Rust async?
- How to determine concurrency threads while using shoryuken for background jobs?
- How to determine stream end in a asynchronous socket server
- How to deploy a war file in Tomcat 7
- How to deserialize records from Kafka using Structured Streaming in Java?
- How to dispatch code blocks to the same thread in iOS?
- How to dispose TransactionScope in cancelable async/await?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.