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.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
Mastering Java Locks: From Optimistic to Biased – A Complete Guide

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 Lock

assumes 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 Lock

assumes 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 Lock

makes 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 Lock

allows 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 Lock

uses 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 Lock

grants lock acquisition in FIFO order, preventing thread starvation.

Unfair Lock

Unfair Lock

allows threads to acquire the lock out of order, offering higher throughput but possible starvation.

Shared Lock

Shared Lock

is essentially the read lock of ReentrantReadWriteLock, permitting multiple threads to hold the lock simultaneously.

Exclusive Lock

Exclusive Lock

permits 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

synchronized

is 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

ReentrantLock

offers manual lock management, interruptibility, fairness options, and condition support, while synchronized is simpler, JVM‑managed, and always non‑fair.

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.

performanceSynchronizationLocks
Su San Talks Tech
Written by

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.

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.