---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 12 Edition"
java_version: 12
chapter: "Chapter 17. Threads and Locks"
section: "17.4"
heading: "17.4. Memory Model"
parent: "Chapter 17. Threads and Locks"
source: https://docs.oracle.com/javase/specs/jls/se12/html/jls-17.html#jls-17.4
id: "17.4"
url: https://jvm.obfuscated.software/jls/se12/17.4.html
raw: https://jvm.obfuscated.software/jls/se12/17.4.md
---

# 17.4. Memory Model

A *memory model* describes, given a program and an execution trace of that program, whether the execution trace is a legal execution of the program. The Java programming language memory model works by examining each read in an execution trace and checking that the write observed by that read is valid according to certain rules.

The memory model describes possible behaviors of a program. An implementation is free to produce any code it likes, as long as all resulting executions of a program produce a result that can be predicted by the memory model.

This provides a great deal of freedom for the implementor to perform a myriad of code transformations, including the reordering of actions and removal of unnecessary synchronization.

**Example 17.4-1. Incorrectly Synchronized Programs May Exhibit Surprising Behavior**

The semantics of the Java programming language allow compilers and microprocessors to perform optimizations that can interact with incorrectly synchronized code in ways that can produce behaviors that seem paradoxical. Here are some examples of how incorrectly synchronized programs may exhibit surprising behaviors.

Consider, for example, the example program traces shown in Table 17.4-A. This program uses local variables `r1` and `r2` and shared variables `A` and `B`. Initially, `A == B == 0`.

**Table 17.4-A. Surprising results caused by statement reordering - original code**

| Thread 1 | Thread 2 |
|---|---|
| 1: `r2 = A;` | 3: `r1 = B;` |
| 2: `B = 1;` | 4: `A = 2;` |

It may appear that the result `r2 == 2` and `r1 == 1` is impossible. Intuitively, either instruction 1 or instruction 3 should come first in an execution. If instruction 1 comes first, it should not be able to see the write at instruction 4. If instruction 3 comes first, it should not be able to see the write at instruction 2.

If some execution exhibited this behavior, then we would know that instruction 4 came before instruction 1, which came before instruction 2, which came before instruction 3, which came before instruction 4. This is, on the face of it, absurd.

However, compilers are allowed to reorder the instructions in either thread, when this does not affect the execution of that thread in isolation. If instruction 1 is reordered with instruction 2, as shown in the trace in Table 17.4-B, then it is easy to see how the result `r2 == 2` and `r1 == 1` might occur.

**Table 17.4-B. Surprising results caused by statement reordering - valid compiler transformation**

| Thread 1 | Thread 2 |
|---|---|
| `B = 1;` | `r1 = B;` |
| `r2 = A;` | `A = 2;` |

To some programmers, this behavior may seem "broken". However, it should be noted that this code is improperly synchronized:

- there is a write in one thread,
- a read of the same variable by another thread,
- and the write and read are not ordered by synchronization.

This situation is an example of a *data race* ([§17.4.5](17.4.5.md)). When code contains a data race, counterintuitive results are often possible.

Several mechanisms can produce the reordering in Table 17.4-B. A Just-In-Time compiler in a Java Virtual Machine implementation may rearrange code, or the processor. In addition, the memory hierarchy of the architecture on which a Java Virtual Machine implementation is run may make it appear as if code is being reordered. In this chapter, we shall refer to anything that can reorder code as a *compiler*.

Another example of surprising results can be seen in Table 17.4-C. Initially, `p == q` and `p.x == 0`. This program is also incorrectly synchronized; it writes to shared memory without enforcing any ordering between those writes.

**Table 17.4-C. Surprising results caused by forward substitution**

| Thread 1 | Thread 2 |
|---|---|
| `r1 = p;` | `r6 = p;` |
| `r2 = r1.x;` | `r6.x = 3;` |
| `r3 = q;` |  |
| `r4 = r3.x;` |  |
| `r5 = r1.x;` |  |

One common compiler optimization involves having the value read for `r2` reused for `r5`: they are both reads of `r1.x` with no intervening write. This situation is shown in Table 17.4-D.

**Table 17.4-D. Surprising results caused by forward substitution**

| Thread 1 | Thread 2 |
|---|---|
| `r1 = p;` | `r6 = p;` |
| `r2 = r1.x;` | `r6.x = 3;` |
| `r3 = q;` |  |
| `r4 = r3.x;` |  |
| `r5 = r2;` |  |

Now consider the case where the assignment to `r6.x` in Thread 2 happens between the first read of `r1.x` and the read of `r3.x` in Thread 1. If the compiler decides to reuse the value of `r2` for the `r5`, then `r2` and `r5` will have the value `0`, and `r4` will have the value `3`. From the perspective of the programmer, the value stored at `p.x` has changed from `0` to `3` and then changed back.

The memory model determines what values can be read at every point in the program. The actions of each thread in isolation must behave as governed by the semantics of that thread, with the exception that the values seen by each read are determined by the memory model. When we refer to this, we say that the program obeys *intra-thread semantics*. Intra-thread semantics are the semantics for single-threaded programs, and allow the complete prediction of the behavior of a thread based on the values seen by read actions within the thread. To determine if the actions of thread *t* in an execution are legal, we simply evaluate the implementation of thread *t* as it would be performed in a single-threaded context, as defined in the rest of this specification.

Each time the evaluation of thread *t* generates an inter-thread action, it must match the inter-thread action *a* of *t* that comes next in program order. If *a* is a read, then further evaluation of *t* uses the value seen by *a* as determined by the memory model.

This section provides the specification of the Java programming language memory model except for issues dealing with `final` fields, which are described in [§17.5](17.5.md).

The memory model specified herein is not fundamentally based in the object-oriented nature of the Java programming language. For conciseness and simplicity in our examples, we often exhibit code fragments without class or method definitions, or explicit dereferencing. Most examples consist of two or more threads containing statements with access to local variables, shared global variables, or instance fields of an object. We typically use variables names such as `r1` or `r2` to indicate variables local to a method or thread. Such variables are not accessible by other threads.

**Sections:**

- [17.4.1. Shared Variables](17.4.1.md)
- [17.4.2. Actions](17.4.2.md)
- [17.4.3. Programs and Program Order](17.4.3.md)
- [17.4.4. Synchronization Order](17.4.4.md)
- [17.4.5. Happens-before Order](17.4.5.md)
- [17.4.6. Executions](17.4.6.md)
- [17.4.7. Well-Formed Executions](17.4.7.md)
- [17.4.8. Executions and Causality Requirements](17.4.8.md)
- [17.4.9. Observable Behavior and Nonterminating Executions](17.4.9.md)
