NOTE

Optimizing Java Locking

Durable lock-performance principles: reduce contention and critical sections, avoid blocking work while locked, split ownership carefully, and treat JVM lock internals as version-sensitive.

JavaCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. Optimize Contention, Not Syntax

Lock performance is dominated by how many threads contend and how long the lock is held.

High-value techniques:

  • shrink critical sections;
  • move I/O and slow calls outside locks;
  • reduce shared mutable state;
  • partition state/locks when invariants permit it;
  • use immutable/snapshot data for read-heavy paths.

2. Lock Coarsening vs. Splitting

Too many tiny repeated acquisitions can add overhead, so coarsening may help when the same lock is repeatedly acquired in a short region.

Conversely, one global lock may serialize unrelated state, so lock splitting/striping can increase concurrency.

The correct granularity follows the protected invariant.

3. JVM Optimizations Change

Historical HotSpot discussions often describe biased locking, lightweight locks, spinning, and inflated monitors as one fixed progression. Those implementation details have changed significantly across JDK releases; biased locking in particular is no longer a general modern baseline.

Do not write correctness logic around object-header/Mark Word states.

4. Measure

Use Java Flight Recorder, async-profiler, thread dumps, and application latency metrics to locate actual lock contention before redesigning synchronization.

Loading helpful count