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

# 17.4.3. Programs and Program Order

Among all the inter-thread actions performed by each thread *t*, the *program order* of *t* is a total order that reflects the order in which these actions would be performed according to the intra-thread semantics of *t*.

A set of actions is *sequentially consistent* if all actions occur in a total order (the execution order) that is consistent with program order, and furthermore, each read *r* of a variable *v* sees the value written by the write *w* to *v* such that:

- *w* comes before *r* in the execution order, and
- there is no other write *w*' such that *w* comes before *w*' and *w*' comes before *r* in the execution order.

Sequential consistency is a very strong guarantee that is made about visibility and ordering in an execution of a program. Within a sequentially consistent execution, there is a total order over all individual actions (such as reads and writes) which is consistent with the order of the program, and each individual action is atomic and is immediately visible to every thread.

If a program has no data races, then all executions of the program will appear to be sequentially consistent.

Sequential consistency and/or freedom from data races still allows errors arising from groups of operations that need to be perceived atomically and are not.

If we were to use sequential consistency as our memory model, many of the compiler and processor optimizations that we have discussed would be illegal. For example, in the trace in [Table 17.4-C](17.4.md), as soon as the write of `3` to `p.x` occurred, subsequent reads of that location would be required to see that value.
