Classification and Explanation of Java Locks for Interview Preparation

This article summarizes the main types of Java locks—including fair/unfair, reentrant, exclusive/shared, mutex/read‑write, optimistic/pessimistic, segment, biased/lightweight/heavyweight, and spin locks—provides brief definitions, usage notes, and a code example to help interview candidates answer concurrency questions effectively.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Classification and Explanation of Java Locks for Interview Preparation

This article introduces various classifications of locks commonly discussed in Java concurrency and interview contexts, aiming to provide concise definitions suitable for quick interview answers.

Fair lock / Unfair lock : A fair lock grants access to threads in the order they request it, while an unfair lock may allow later‑arriving threads to acquire the lock first, potentially causing priority inversion or starvation. In Java, ReentrantLock can be constructed as fair (default is unfair), and synchronized is inherently unfair.

Reentrant lock (also called recursive lock): The same thread can acquire the lock multiple times without deadlocking. Both ReentrantLock and synchronized are reentrant. Example:

synchronized void setA() throws Exception {
    Thread.sleep(1000);
    setB();
}

synchronized void setB() throws Exception {
    Thread.sleep(1000);
}

The above demonstrates that a thread holding the lock in setA can re‑enter the lock in setB, avoiding deadlock.

Exclusive lock / Shared lock : An exclusive lock can be held by only one thread at a time, whereas a shared lock can be held by multiple threads simultaneously. ReentrantLock is exclusive; ReadWriteLock provides a shared read lock and an exclusive write lock.

Mutex lock / Read‑Write lock : These are concrete implementations of the above concepts. In Java, ReentrantLock is a mutex lock, and ReadWriteLock implements read‑write locking.

Optimistic lock / Pessimistic lock : These describe strategies rather than concrete lock types. Pessimistic locking assumes conflicts and locks data preemptively; optimistic locking assumes conflicts are rare and uses techniques like CAS (compare‑and‑swap) to retry updates without locking.

Segment lock : Not a separate lock type but a design used in ConcurrentHashMap. The map is divided into segments, each protected by its own lock (a ReentrantLock), allowing concurrent updates to different segments while still providing thread safety.

Biased / Lightweight / Heavyweight lock : These are lock states used by the JVM for synchronized. A biased lock is fast when a single thread repeatedly acquires it; it upgrades to a lightweight lock when contention appears, and further upgrades to a heavyweight lock (causing thread blocking) if contention persists.

Spin lock : A lock where a thread repeatedly attempts to acquire the lock in a loop (spins) instead of blocking, reducing context‑switch overhead at the cost of CPU consumption. Typical implementations rely on CAS loops.

Source: cnblogs.com/lxmyhappy/p/7380073.html

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.

JavaconcurrencyinterviewLocksReentrantLock
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

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.