Fundamentals 17 min read

Comprehensive Overview of Java Locks and Concurrency Mechanisms

This article provides a detailed guide to various Java lock types—including optimistic, pessimistic, spin, reentrant, read‑write, fair, unfair, shared, exclusive, heavyweight, lightweight, biased, segment, mutex, synchronization, deadlock, lock coarsening, and lock elimination—explaining their principles, typical usages, advantages, disadvantages, and related JVM optimizations.

Wukong Talks Architecture
Wukong Talks Architecture
Wukong Talks Architecture
Comprehensive Overview of Java Locks and Concurrency Mechanisms

The article presents a systematic summary of Java concurrency locks, describing each lock type, its underlying principle, typical application scenarios, and performance characteristics.

1. Optimistic Lock

Optimistic lock assumes a read‑many/write‑few environment; it does not acquire a lock during reads and validates the value during writes using CAS (compare‑and‑swap). If the current value matches the expected value, the update proceeds atomically.

2. Pessimistic Lock

Pessimistic lock assumes frequent writes and acquires a lock on every read/write operation. In Java it is implemented with synchronized or ReentrantLock, blocking other threads until the lock is released.

3. Spin Lock

Spin lock makes a thread repeatedly execute a busy‑loop while waiting for the lock, avoiding costly context switches. It is implemented in Java by repeatedly attempting CAS until success. Adaptive spinning adjusts the spin count based on previous lock acquisition history.

4. Reentrant (Recursive) Lock

A reentrant lock allows the same thread to acquire the lock multiple times without deadlocking. Java provides ReentrantLock and the intrinsic synchronized mechanism for this purpose.

5. Read‑Write Lock

Read‑write lock separates read and write access using ReentrantReadWriteLock. Multiple threads can hold the read lock concurrently, while the write lock is exclusive.

Example usage:

/**
 * Create a read‑write lock
 */
private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();

// Acquire read lock
rwLock.readLock().lock();
// Release read lock
rwLock.readLock().unlock();

// Acquire write lock
rwLock.writeLock().lock();
// Release write lock
rwLock.writeLock().unlock();

6. Fair Lock

A fair lock grants access to threads in the order they requested the lock (FIFO), preventing starvation.

7. Unfair Lock

An unfair lock does not guarantee ordering; threads may acquire the lock out of request order, offering higher throughput but possible thread starvation.

8. Shared Lock

Shared lock allows multiple threads to hold the lock simultaneously for reading, similar to the read side of a read‑write lock.

9. Exclusive Lock

Exclusive lock permits only one thread to hold the lock at a time, analogous to a traditional mutex.

10. Heavyweight Lock

Heavyweight lock relies on the operating system's mutex; in Java it is represented by synchronized when contention forces escalation.

11. Lightweight Lock

Lightweight lock uses CAS to avoid OS mutexes when there is no contention, providing better performance under low contention.

12. Biased Lock

Biased lock eliminates synchronization entirely for a thread that repeatedly acquires the same lock without contention, reducing overhead.

13. Segment Lock

Segment lock is used by ConcurrentHashMap, which partitions the map into multiple segments, each protected by its own lock, allowing higher concurrency.

14. Mutex Lock

Mutex lock ensures exclusive access to a resource, synonymous with pessimistic and exclusive locks.

15. Synchronization Lock

Synchronization lock (often synchronized) enforces mutual exclusion for critical sections.

16. Deadlock

Deadlock occurs when two or more threads hold resources the other needs, causing all to wait indefinitely.

17. Lock Coarsening

Lock coarsening expands the lock scope to cover a larger code region, reducing the frequency of lock acquisition/release.

18. Lock Elimination

Lock elimination removes unnecessary locks when the JVM determines that the protected data does not escape the thread.

19. synchronized Keyword

The synchronized keyword provides intrinsic locking for methods or blocks, acting as an exclusive, reentrant, and non‑fair lock.

20. Differences Between Lock and synchronized

Lock

is an interface requiring explicit lock/unlock calls, supports interruptible acquisition, and can implement fair ordering; synchronized is a language construct that automatically acquires and releases the lock.

21. Differences Between ReentrantLock and synchronized

Both provide reentrant mutual exclusion, but ReentrantLock offers explicit control, interruptibility, condition variables, and configurable fairness, whereas synchronized is simpler and managed by the JVM.

Index

Lock Name

Application

1

Optimistic Lock

CAS

2

Pessimistic Lock

synchronized, Vector, Hashtable

3

Spin Lock

CAS

4

Reentrant Lock

synchronized, ReentrantLock, Lock

5

Read‑Write Lock

ReentrantReadWriteLock, CopyOnWriteArrayList, CopyOnWriteArraySet

6

Fair Lock

ReentrantLock(true)

7

Unfair Lock

synchronized, ReentrantLock(false)

8

Shared Lock

Read lock of ReentrantReadWriteLock

9

Exclusive Lock

synchronized, Vector, Hashtable, write lock of ReentrantReadWriteLock

10

Heavyweight Lock

synchronized

11

Lightweight Lock

Lock optimization techniques

12

Biased Lock

Lock optimization techniques

13

Segment Lock

ConcurrentHashMap

14

Mutex Lock

synchronized

15

Synchronization Lock

synchronized

16

Deadlock

Mutual resource waiting

17

Lock Coarsening

Lock optimization techniques

18

Lock Elimination

Lock optimization techniques

The article also includes interview questions about reentrant locks and practical tips for avoiding common pitfalls in Java concurrency.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaperformanceconcurrencymultithreadingLocks
Wukong Talks Architecture
Written by

Wukong Talks Architecture

Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.