Java.Multithreading.What is ordering, as-if-serial semantics, sequential consistency, visibility, atomicity, happens-before, mutual exclusion, safe publication?

🧩 1. Ordering

Ordering is about the sequence of execution of operations (like reads and writes). It matters because compilers, JVM, and CPUs may reorder instructions to optimize performance.

x = 1;
y = 2;

You’d assume x is set before y, but the JVM might reorder this if it thinks it’s safe to do so — unless there’s a rule (like happens-before) that prevents it.

🧠 2. As-If-Serial Semantics

This is a guarantee by the Java compiler:

Your program will behave as if all operations happened in the exact order you wrote them, unless there’s no visible effect.

🧪 Example:

int a = 1;
int b = a + 2;

The compiler can combine or reorder this into:

int b = 3;

✅ But it can’t reorder writes/reads across threads unless it’s safe under the Java Memory Model.

🔁 3. Sequential Consistency

The result of execution is the same as if operations were executed one at a time in a global order, consistent with each thread’s code.

This is the most intuitive and strict memory model.

🧠 Java does NOT guarantee full sequential consistency by default, because it allows reordering for performance.

To enforce it, you need:

  • synchronized
  • volatile
  • other happens-before mechanisms

👀 4. Visibility

Visibility means:

When one thread updates a variable, do other threads see that update?

By default, threads may cache values and not see updates from others.

❌ Without volatile or synchronized:

flag = true; // Thread A

if (flag) {  // Thread B
   // Might still see false!
}

✅ With volatile:

The change becomes visible immediately across threads.


🎯 5. Atomicity

Atomicity means:

An operation is indivisible — it either happens completely or not at all.

Examples:

  • int x = 1; — ✅ Atomic
  • x++ — ❌ Not atomic (it’s read + add + write)

🧰 For atomicity in Java, use:

  • synchronized
  • AtomicInteger, etc.
  • Locks like ReentrantLock

🔁 6. Happens-Before Relationship

This defines legal orderings and visibility guarantees between operations in different threads.

If A happens-before B, then:

  • A’s changes are visible to B
  • B sees A as completed

Happens-before examples:

  • Writing to a volatile variable happens-before reading it.
  • Lock release happens-before lock acquire.
  • Thread start happens-before its first action.

🔒 7. Mutual Exclusion

This is about ensuring that only one thread at a time can access a critical section of code.

You achieve it with:

  • synchronized
  • ReentrantLock
  • Semaphores, etc.

This prevents race conditions and ensures atomicity and visibility of shared state.

✅ 8. Safe Publication

Safe publication means:

An object is safely visible to other threads after it is fully constructed and initialized.

Unsafe publication can cause other threads to see a half-initialized object 😱

Unsafe:

MyObject obj = new MyObject(); // other thread sees this

Safe ways:

  • Use final fields properly
  • Publish through a volatile or synchronized block
  • Use static initializers
public static final MyObject obj = new MyObject(); // ✅ safe publication

🧠 The Big Picture: How They Relate

           +-----------------+
           | Ordering        |
           +--------+--------+
                    |
   +----------------v----------------+
   | As-if-serial + Happens-before   |
   +--------+------------------------+
            |
     +------v------+      +--------------+
     | Visibility   |<----| Safe Pub.    |
     +--------------+     +--------------+
            ^
     +------v------+
     | Atomicity    |
     +--------------+
            ^
     +------v--------+
     | Mutual Excl.  |
     +---------------+

🚀 Summary Table

ConceptMeaning
OrderingSequence of operations (may be changed for optimization)
As-if-serialCompiler preserves program behavior even with optimization
Sequential ConsistencyA strict memory model where all threads see operations in same order
VisibilityWhen a thread sees updates made by another
AtomicityOperation happens completely or not at all
Happens-beforeRule that defines when effects of one action are visible to another
Mutual ExclusionOnly one thread accesses a critical section at a time
Safe PublicationEnsuring fully initialized objects are visible to other threads
This entry was posted in Без рубрики. Bookmark the permalink.