skip to content

Why does even an empty synchronized block carry memory-barrier (acquire/release) effects, and what does this reveal about where Java's visibility guarantee comes from?

level: seniorimportance: should knowfreq 38%

answer

  1. barrier attaches to acquire/release events, not the body
  2. acquire = invalidate + re-read; release = flush prior writes
  3. empty block still orders same-lock threads
  4. visibility is a property of the protocol, not the critical section
  5. modern code: use volatile / VarHandle fences instead

basics

~20 s

The visibility comes from taking and releasing the lock, not from the code inside. An empty block's release still publishes the thread's earlier writes, and its acquire still sees earlier holders' writes, but only between threads that lock the same object.

solid answer

~50 s

The acquire and release effects are attached to the monitor operations themselves, not to the statements in the block. Entering performs a monitor acquire: no later read may move before it or reuse a value read earlier. Exiting performs a monitor release: no earlier write may move after it. Since both fire at the boundary, an empty `synchronized (lock) {}` still publishes everything the thread wrote before it to the next thread that locks `lock`, and still lets this thread see what earlier holders of `lock` wrote. It is not a general fence: a thread that never locks `lock` gets no guarantee, and the JLS lets an implementation remove synchronization no other thread can observe. This reveals the core truth: Java's cross-thread visibility is produced by crossing the monitor boundary and establishing the monitor-lock happens-before edge, not by doing work under the lock.

go deeper

for a junior

May not yet know this nuance; enough to recognize that synchronized affects visibility and that locking, not the code inside, is what matters for sharing data.

for a middle

Can state that entering re-reads and exiting flushes, and infer that an empty block still does both because the effect is on the lock operations.

for a senior

Explains barriers are bound to acquire/release events, articulates 'visibility is a property of the protocol not the body,' and knows it is a conceptual test rather than a coding pattern.

for a principal

Ties it to JSR-133 semantics, volatile/VarHandle fences, lock elision and coarsening, and can advise teams on correct fencing idioms and why empty-block fencing is an anti-pattern.

## Setting the stage: barriers vs. block bodies To see why an empty `synchronized` block still 'does something', you must understand that the memory guarantees attach to two **events** — acquiring a monitor and releasing it — and not to the statements between them. ### Define the events - **Monitor acquire** happens on entry to a `synchronized` region. The JVM marks the object's lock as held by this thread (blocking if another thread holds it). - **Monitor release** happens on exit (normal or exceptional). The JVM frees the lock. ### Define the barriers A **memory barrier** (a.k.a. fence) is an instruction/semantic that restricts how reads and writes may be reordered and when they become visible: - An **acquire barrier** says: operations *after* it cannot be moved to *before* it, so the thread cannot reuse a value it read, or kept in a register, before the barrier. "Invalidating the cache" is the informal picture; the JLS states only the ordering and leaves the mechanism to the implementation. - A **release barrier** says: operations *before* it cannot be moved to *after* it, and all those prior writes are made visible (flushed) before the barrier completes. The JMM binds an **acquire barrier to monitor acquire** and a **release barrier to monitor release**. That binding is the whole point: it is the *act of acquiring/releasing*, independent of any body, that carries the effect. ### Therefore: the empty block Consider: ```java synchronized (lock) { } ``` This is `acquire; release` with nothing in between. The acquire still orders this thread after the previous holder's release; the release still publishes prior writes to the next holder. So if thread A had earlier mutated shared fields (anywhere before this point, by program order) and then runs this empty block, the release publishes those writes. A thread B that subsequently does its own `synchronized (lock) { }` performs an acquire and is guaranteed (by the **monitor-lock happens-before rule**: an unlock happens-before every later lock of the same monitor) to observe A's published writes. Two limits keep this precise: - The effect exists only between threads that lock the **same** object. A thread that reads the fields without locking `lock` gets no guarantee from A's empty block. - The JLS (17.4) allows the "removal of unnecessary synchronization". If no other thread can ever lock the object, as with `synchronized (new Object()) {}`, the block can disappear entirely, because there is no other locker to order against. ### What this reveals It reveals that **visibility in Java is a property of the synchronization protocol, not of the work done inside the critical section.** People sometimes imagine the lock 'protects the variables you touch inside.' More precisely, the lock establishes ordering edges; what becomes visible is *everything before the release*, surfacing to *everything after a matching acquire*. The body is where you *use* mutual exclusion; the boundary is where you *get* the memory effect. ### Idiom note An empty block is sometimes offered as a cheap fence. It is not one: its effect reaches only threads that lock the same object, and it hides the intent. When you genuinely need an explicit barrier, prefer `volatile`, `final`, `java.util.concurrent` primitives, or the `VarHandle` fences (`fullFence`, `acquireFence`, `releaseFence`, since JDK 9). The empty-block fact is best treated as a *conceptual* test of where the guarantee lives, not a coding recommendation. ### Relationship to volatile `volatile` gives a similar acquire (on read) / release (on write) pairing without mutual exclusion. A volatile write is like a lightweight release; a volatile read is like a lightweight acquire. So the same 'boundary, not body' principle underlies volatile too — there is no body at all, just the single field access carrying the barrier. ### Caveat: don't conclude empty blocks are useful The JIT may remove a lock on an object that never escapes its thread, and HotSpot's C2 compiler coarsens adjacent locks by default (`EliminateLocks`), but only in ways that keep every ordering another thread could observe through the same lock. Regardless, relying on empty blocks for fencing is fragile and unclear; the takeaway is understanding, not a technique.

  • If empty synchronized blocks carry barriers, what is the modern, clearer way to express an explicit memory fence in Java?
    Use volatile fields for the common publish/observe pattern, or VarHandle.fullFence()/acquireFence()/releaseFence() (since JDK 9) when you need a standalone fence without a lock; sun.misc.Unsafe's fence methods are deprecated for removal since JDK 22. These state intent directly instead of abusing an empty critical section.
  • How does a volatile field relate to the 'boundary, not body' idea?
    A volatile write behaves like a release barrier and a volatile read like an acquire barrier, with no block at all — the single field access carries the barrier. It's the same principle: the guarantee rides on the synchronization action, not on surrounding work.

  • Claiming an empty synchronized block on a lock other threads use has no memory effect
  • Saying the lock only protects variables written inside the block
  • Recommending empty synchronized blocks as the right way to fence in modern code
  • Confusing the acquire effect (entry) with the release effect (exit)
  • Treating an empty synchronized block as a fence for threads that never take that lock

context