Mastering Java Locks: From Optimistic to Biased – A Complete Guide
This article provides a comprehensive overview of Java's lock mechanisms—including optimistic, pessimistic, spin, reentrant, read‑write, fair, unfair, shared, exclusive, heavyweight, lightweight, biased, segment, mutex, and synchronized locks—explaining their principles, typical usages, performance trade‑offs, and key differences with code examples.
This article summarizes the most commonly used Java lock types, their underlying principles, typical applications, and practical differences, helping developers choose the right synchronization mechanism for their concurrent programs.
Optimistic Lock
Optimistic Lockassumes a read‑mostly scenario; reads proceed without locking, and writes succeed only if the current value matches the expected value, typically implemented with CAS in Java.
Pessimistic Lock
Pessimistic Lockassumes frequent writes; every read or write acquires a lock, blocking other threads until the lock is released. In Java it is realized by synchronized and ReentrantLock.
Spin Lock
Spin Lockmakes a thread repeatedly loop (spin) while waiting for a lock, avoiding costly thread suspension. It is implemented in Java by spinning on a failed CAS operation. Adaptive spinning adjusts the spin duration based on previous attempts.
Reentrant (Recursive) Lock
Reentrant Lockallows a thread that already holds the lock to acquire it again without blocking. It is implemented by ReentrantLock and synchronized in Java.
Interview Q1: If a reentrant lock is acquired twice but released once, the thread will deadlock.
Interview Q2: Releasing a lock twice when it was acquired once throws java.lang.IllegalMonitorStateException.
Read‑Write Lock
Read‑Write Lockuses ReentrantReadWriteLock to allow multiple concurrent readers while writes are exclusive. Example usage:
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();Fair Lock
Fair Lockgrants lock acquisition in FIFO order, preventing thread starvation.
Unfair Lock
Unfair Lockallows threads to acquire the lock out of order, offering higher throughput but possible starvation.
Shared Lock
Shared Lockis essentially the read lock of ReentrantReadWriteLock, permitting multiple threads to hold the lock simultaneously.
Exclusive Lock
Exclusive Lockpermits only one thread to hold the lock, similar to synchronized or ReentrantLock.
Heavyweight Lock
Implemented by synchronized using the operating system's mutex; incurs high context‑switch cost.
Lightweight Lock
Introduced in JDK6; uses CAS to avoid OS mutex when there is no contention.
Biased Lock
Also added in JDK6; eliminates synchronization entirely for a thread that repeatedly acquires the same lock without contention.
Segment Lock
Used by ConcurrentHashMap; the map is divided into multiple segments, each protected by its own lock, allowing high concurrency.
Mutex Lock
Another term for exclusive or pessimistic locks; only one thread may access the protected resource.
Synchronized vs. Lock
synchronizedis a language keyword providing built‑in locking, automatically releasing on exceptions, but cannot be interrupted. Lock is an interface requiring explicit lock() and unlock(), supports interruptible acquisition, fairness policies, and multiple condition variables.
ReentrantLock vs. synchronized
ReentrantLockoffers manual lock management, interruptibility, fairness options, and condition support, while synchronized is simpler, JVM‑managed, and always non‑fair.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
