Understanding Java Locks: Optimistic, Pessimistic, CAS, and Various Lock Types

The article explains Java’s various lock mechanisms—including optimistic vs. pessimistic, spin and adaptive spin, no‑lock through heavyweight states, fair vs. unfair, reentrant vs. non‑reentrant, and exclusive vs. shared locks—using JDK 8 source code and practical examples to guide developers in choosing the appropriate lock for different concurrency scenarios.

Meituan Technology Team
Meituan Technology Team
Meituan Technology Team
Understanding Java Locks: Optimistic, Pessimistic, CAS, and Various Lock Types

Preface

Concurrent programming is a fundamental skill for Java developers. This article provides an in‑depth analysis of Java lock mechanisms, using JDK 8 source code and practical usage scenarios to introduce mainstream locks and their appropriate applications.

1. Optimistic Lock vs. Pessimistic Lock

Optimistic locks assume that concurrent updates are rare and avoid acquiring a lock during the read phase, checking for conflicts only when updating (e.g., via CAS). Pessimistic locks acquire a lock before accessing the data to prevent other threads from modifying it. Optimistic locks are suitable for read‑heavy workloads, while pessimistic locks fit write‑heavy scenarios.

2. Spin Lock vs. Adaptive Spin Lock

Spin locks keep a thread busy looping while waiting for a lock, avoiding the overhead of thread suspension and wake‑up. Adaptive spin locks adjust the spin count based on previous contention and the lock holder's state, extending spin time when it is likely to succeed.

3. No‑Lock, Biased Lock, Lightweight Lock, Heavyweight Lock

These are four lock states for synchronized. A lock can only transition upward (no‑lock → biased → lightweight → heavyweight). The article explains the object header (Mark Word and Klass Pointer) and how each state is represented.

4. Fair Lock vs. Unfair Lock

Fair locks grant access to threads in the order they requested the lock, preventing starvation but reducing throughput. Unfair locks allow a thread to acquire the lock immediately if it becomes available, improving performance at the risk of starvation.

5. Reentrant Lock vs. Non‑Reentrant Lock

Reentrant (recursive) locks allow the same thread to acquire the same lock multiple times, preventing deadlocks in nested calls. Non‑reentrant locks would block the thread on the second acquisition, potentially causing deadlock.

6. Exclusive Lock vs. Shared Lock

Exclusive locks (e.g., ReentrantLock) allow only one thread to hold the lock, while shared locks (e.g., the read lock of ReentrantReadWriteLock) permit multiple threads to read concurrently. The article shows how AQS encodes lock state, with the high 16 bits for read count and low 16 bits for write count.

Conclusion

The article introduced the most common Java locks, compared them from source‑code and practical perspectives, and emphasized the importance of understanding underlying mechanisms to choose the right lock for a given scenario.

References

1. 《Java并发编程艺术》 2. Java中的锁 3. Java并发:关键字synchronized解析 4. 深入理解读写锁:ReadWriteLock源码分析 5. Java synchronized原理总结 6. Java CAS 原理剖析 7. 聊聊并发(二):Java SE1.6中的Synchronized 8. 【JUC】JDK1.8源码分析之ReentrantReadWriteLock 9. Java多线程(十)之ReentrantReadWriteLock深入分析 10. Java:读写锁的实现原理

Author

Jia Qi, Backend Engineer at Meituan Dianping. Joined in 2017, responsible for domestic vacation business development.

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.

JavaconcurrencyCASLocksReentrantLockthread synchronizationReadWriteLock
Meituan Technology Team
Written by

Meituan Technology Team

Over 10,000 engineers powering China’s leading lifestyle services e‑commerce platform. Supporting hundreds of millions of consumers, millions of merchants across 2,000+ industries. This is the public channel for the tech teams behind Meituan, Dianping, Meituan Waimai, Meituan Select, and related services.

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.