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

# 17.4.8. Executions and Causality Requirements

We use *f*|_d to denote the function given by restricting the domain of *f* to *d*. For all *x* in *d*, *f*|_d(*x*) = *f*(*x*), and for all *x* not in *d*, *f*|_d(*x*) is undefined.

We use *p*|_d to represent the restriction of the partial order *p* to the elements in *d*. For all *x*,*y* in *d*, *p*(*x*,*y*) if and only if *p*|_d(*x*,*y*). If either *x* or *y* are not in *d*, then it is not the case that *p*|_d(*x*,*y*).

A well-formed execution *E* = < *P, A, po, so, W, V, sw, hb* > is validated by *committing* actions from *A*. If all of the actions in *A* can be committed, then the execution satisfies the causality requirements of the Java programming language memory model.

Starting with the empty set as *C_0*, we perform a sequence of steps where we take actions from the set of actions *A* and add them to a set of committed actions *C_i* to get a new set of committed actions *C_{i+1}*. To demonstrate that this is reasonable, for each *C_i* we need to demonstrate an execution *E* containing *C_i* that meets certain conditions.

Formally, an execution *E satisfies the causality requirements of the Java programming language memory model* if and only if there exist:

- Sets of actions *C_0*, *C_1*, ... such that:

  - *C_0* is the empty set
  - *C_i* is a proper subset of *C_{i+1}*
  - *A* = ∪ (*C_0*, *C_1*, ...)

  If *A* is finite, then the sequence *C_0*, *C_1*, ... will be finite, ending in a set *C_n* = *A*.

  If *A* is infinite, then the sequence *C_0*, *C_1*, ... may be infinite, and it must be the case that the union of all elements of this infinite sequence is equal to *A*.

- Well-formed executions *E_1*, ..., where *E_i* = < *P, A_i, po_i, so_i, W_i, V_i, sw_i, hb_i* >.

Given these sets of actions *C_0*, ... and executions *E_1*, ... , every action in *C_i* must be one of the actions in *E_i*. All actions in *C_i* must share the same relative happens-before order and synchronization order in both *E_i* and *E*. Formally:

1. *C_i* is a subset of *A_i*
2. *hb_i*|_{C_i} = *hb*|_{C_i}
3. *so_i*|_{C_i} = *so*|_{C_i}

The values written by the writes in *C_i* must be the same in both *E_i* and *E*. Only the reads in *C_{i-1}* need to see the same writes in *E_i* as in *E*. Formally:

4. *V_i*|_{C_i} = *V*|_{C_i}
5. *W_i*|_{C_{i-1}} = *W*|_{C_{i-1}}

All reads in *E_i* that are not in *C_{i-1}* must see writes that happen-before them. Each read *r* in *C_i* - *C_{i-1}* must see writes in *C_{i-1}* in both *E_i* and *E*, but may see a different write in *E_i* from the one it sees in *E*. Formally:

6. For any read *r* in *A_i* - *C_{i-1}*, we have *hb_i(W_i(r), r)*
7. For any read *r* in (*C_i* - *C_{i-1}*), we have *W_i(r)* in *C_{i-1}* and *W(r)* in *C_{i-1}*

Given a set of sufficient synchronizes-with edges for *E_i*, if there is a release-acquire pair that happens-before ([§17.4.5](17.4.5.md)) an action you are committing, then that pair must be present in all *E_j*, where *j* ≥ *i*. Formally:

8. Let *ssw_i* be the *sw_i* edges that are also in the transitive reduction of *hb_i* but not in *po*. We call *ssw_i* the *sufficient synchronizes-with edges for E_i*. If *ssw_i(x, y)* and *hb_i(y, z)* and *z* in *C_i*, then *sw_j(x, y)* for all *j* ≥ *i*.

   If an action *y* is committed, all external actions that happen-before *y* are also committed.

9. If *y* is in *C_i*, *x* is an external action and *hb_i(x, y)*, then *x* in *C_i*.

**Example 17.4.8-1. Happens-before Consistency Is Not Sufficient**

Happens-before consistency is a necessary, but not sufficient, set of constraints. Merely enforcing happens-before consistency would allow for unacceptable behaviors - those that violate the requirements we have established for programs. For example, happens-before consistency allows values to appear "out of thin air". This can be seen by a detailed examination of the trace in Table 17.4.8-A.

**Table 17.4.8-A. Happens-before consistency is not sufficient**

| Thread 1 | Thread 2 |
|---|---|
| `r1 = x;` | `r2 = y;` |
| `if (r1 != 0) y = 1;` | `if (r2 != 0) x = 1;` |

The code shown in Table 17.4.8-A is correctly synchronized. This may seem surprising, since it does not perform any synchronization actions. Remember, however, that a program is correctly synchronized if, when it is executed in a sequentially consistent manner, there are no data races. If this code is executed in a sequentially consistent way, each action will occur in program order, and neither of the writes will occur. Since no writes occur, there can be no data races: the program is correctly synchronized.

Since this program is correctly synchronized, the only behaviors we can allow are sequentially consistent behaviors. However, there is an execution of this program that is happens-before consistent, but not sequentially consistent:

```
r1 = x;  // sees write of x = 1
y = 1;
r2 = y;  // sees write of y = 1
x = 1;
```

This result is happens-before consistent: there is no happens-before relationship that prevents it from occurring. However, it is clearly not acceptable: there is no sequentially consistent execution that would result in this behavior. The fact that we allow a read to see a write that comes later in the execution order can sometimes thus result in unacceptable behaviors.

Although allowing reads to see writes that come later in the execution order is sometimes undesirable, it is also sometimes necessary. As we saw above, the trace in [Table 17.4.5-A](17.4.5.md) requires some reads to see writes that occur later in the execution order. Since the reads come first in each thread, the very first action in the execution order must be a read. If that read cannot see a write that occurs later, then it cannot see any value other than the initial value for the variable it reads. This is clearly not reflective of all behaviors.

We refer to the issue of when reads can see future writes as *causality*, because of issues that arise in cases like the one found in Table 17.4.8-A. In that case, the reads cause the writes to occur, and the writes cause the reads to occur. There is no "first cause" for the actions. Our memory model therefore needs a consistent way of determining which reads can see writes early.

Examples such as the one found in Table 17.4.8-A demonstrate that the specification must be careful when stating whether a read can see a write that occurs later in the execution (bearing in mind that if a read sees a write that occurs later in the execution, it represents the fact that the write is actually performed early).

The memory model takes as input a given execution, and a program, and determines whether that execution is a legal execution of the program. It does this by gradually building a set of "committed" actions that reflect which actions were executed by the program. Usually, the next action to be committed will reflect the next action that can be performed by a sequentially consistent execution. However, to reflect reads that need to see later writes, we allow some actions to be committed earlier than other actions that happen-before them.

Obviously, some actions may be committed early and some may not. If, for example, one of the writes in Table 17.4.8-A were committed before the read of that variable, the read could see the write, and the "out-of-thin-air" result could occur. Informally, we allow an action to be committed early if we know that the action can occur without assuming some data race occurs. In Table 17.4.8-A, we cannot perform either write early, because the writes cannot occur unless the reads see the result of a data race.
