Quick revision
The short answer
HotSpot handles an uncontended synchronized with a compare-and-swap on the object's header word (the mark word) or on a small per-thread lock record, with no operating system involvement. When another thread contends, or when wait() is called, the lock is inflated to a full ObjectMonitor that has an owner, an entry queue of blocked threads and a wait set. Contending threads spin briefly and then park, which costs context switches. Biased locking was disabled by default in JDK 15 and removed later, and recent JDKs replaced the old stack-locking scheme with a new lightweight locking implementation; the details and defaults vary by version, so check your JDK.
What to remember
In this answer, we saw that HotSpot keeps lock state in the object's mark word, so an uncontended synchronized costs one CAS, using either legacy stack locking or the newer lightweight locking with a per-thread lock stack. Contention or wait() inflates the lock into an ObjectMonitor with an owner, an entry list and a wait set, and contending threads spin briefly before parking, which costs context switches and shows up as BLOCKED threads in dumps. Recent JDKs disabled and then removed biased locking, replaced stack locking with lightweight locking, and stopped synchronized from pinning virtual threads, with details that depend on the version.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
HotSpot handles an uncontended synchronized with a compare-and-swap on the object's header word (the mark word) or on a small per-thread lock record, with no operating system involvement. When another thread contends, or when wait() is called, the lock is inflated to a full ObjectMonitor that has an owner, an entry queue of blocked threads and a wait set. Contending threads spin briefly and then park, which costs context switches. Biased locking was disabled by default in JDK 15 and removed later, and recent JDKs replaced the old stack-locking scheme with a new lightweight locking implementation; the details and defaults vary by version, so check your JDK.
Why this question is asked
synchronized looks like one keyword, but inside HotSpot it is a small state machine that tries very hard to avoid the operating system. Knowing how it works explains real observations: why uncontended locks are almost free, why contention suddenly makes a service slow, why thread dumps show waiting to lock, and why JDK upgrades changed locking performance. Interviewers ask this at the ten-year level to see whether we can connect those observations to the mechanism. In this answer, we will recap the object header, follow a lock from the uncontended fast path to inflation, see what contended threads do, and go through what changed in recent JDKs.
1A Quick Recap of the Object Header
Every Java object starts with a header. Its first word, the mark word, holds the identity hash code, garbage collector age bits and, importantly here, the lock state. The second part points to the object's class. Because every object can be locked, every object needs room in its header to describe its lock, which is why the JVM designs locking around a few bits in that word.
The lock state encoded in the mark word tells the JVM whether the object is unlocked, locked by one thread in a light way, or inflated, in which case the word points to a separate monitor structure.
2The Uncontended Fast Path
Most locks in most programs are uncontended: one thread takes the lock, does its work and releases it before anyone else wants it. HotSpot makes this path cheap by using a single atomic compare-and-swap instruction (CAS) on the mark word, which either succeeds, meaning the lock is ours, or fails, meaning someone else got there.
In the legacy scheme, called stack locking, the thread copied the mark word into a lock record on its own stack and swung the header to point at that record. In the newer lightweight locking, the thread instead sets lock bits in the header with a CAS and pushes the object onto a small per-thread lock stack, which records what it holds. Either way, no system call happens, so an uncontended lock costs on the order of tens of nanoseconds. Recursive entries by the owner are recorded on the thread side too, which is how reentrancy stays cheap.
3Inflation to an ObjectMonitor
When a second thread tries to lock an object that is already held, a CAS cannot express "one owner and one waiter". So HotSpot inflates the lock: it allocates an ObjectMonitor, a C++ structure inside the JVM, and makes the object's header refer to it. The same happens when the owner calls wait(), because the waiting thread must be recorded somewhere, and in some cases when the identity hash code is needed while the header is in use.
An ObjectMonitor holds the owning thread, a recursion count, a queue of threads trying to enter (the entry list) and a wait set of threads that called wait(). This is the structure behind the textbook description of a monitor.
- Thread enters synchronizedreads the mark word
- Header unlocked?YesCAS to lock it (fast path)NoCheck who holds it
- Held by this thread?Yesrecursive entry, no waitNoContention
- Inflate to ObjectMonitorowner, entry list, wait set
- Spin brieflythe owner may release soon
- Still held: park the threadOS sleep, context switch
- Owner exits, wakes a successorthe woken thread competes again
7 steps in order. Play them, or step through them one at a time.
4What Contended Threads Do
A thread that finds an inflated monitor owned by someone else first spins: it loops for a short, adaptive time, retrying the CAS, because critical sections are often short and the owner may release in a microsecond. Spinning burns CPU, but it avoids the much larger cost of sleeping and waking.
If spinning fails, the thread parks: it joins the entry list and asks the operating system to suspend it. When the owner exits, it wakes a successor, which must be scheduled back onto a CPU and then compete for the lock again. Each park and wake-up is a pair of context switches costing microseconds, plus lost cache warmth. That is why heavy contention on one lock can make a service slow even with idle CPU, and why a thread dump shows many threads in BLOCKED state, waiting to lock the same object.
Notice how the dump reads: many threads waiting to lock the same address, and one thread that has locked it while doing something slow. JFR shows the same thing with jdk.JavaMonitorEnter events, which record contended entries above a duration threshold.
5What Changed in Recent JDKs
Biased locking let the first thread that locked an object bias it to itself, so later locks by that thread needed no CAS at all. Revoking the bias when another thread arrived was expensive and often needed a safepoint, and the code was complex. JEP 374 disabled it by default and deprecated it in JDK 15, and it was removed in a later release (around JDK 18).
Next, the legacy stack-locking scheme was replaced by the lightweight locking described above. It first appeared as an option in JDK 21 (selected with a LockingMode flag) and became the default in later releases, and the legacy mode was then deprecated and removed. Separately, JDK 24 (JEP 491) changed synchronized so that a virtual thread blocked in or on a monitor no longer pins its carrier thread, and work on compact object headers moves monitor information out of the header into a side table. The exact releases and flags differ, so check your JDK's release notes.
| JDK | Change (check your exact version) |
|---|---|
| 15 | biased locking disabled by default and deprecated (JEP 374) |
| 18 | biased locking removed; flag obsolete |
| 21 | new lightweight locking available as an option |
| later (23+) | lightweight locking becomes default; legacy mode phased out |
| 24 | synchronized no longer pins virtual threads (JEP 491) |
6Follow-up questions
Why does wait() force inflation?
A thread that waits must be recorded in a wait set, and must release the monitor while remembering its recursion count. Only the full ObjectMonitor has room for that state; a few bits in the header or a lock stack entry do not.
Do monitors ever deflate again?
Yes. HotSpot periodically deflates idle monitors, those with no owner and no waiters, and returns the object to the lightweight state. In modern JDKs a dedicated thread does this concurrently instead of at safepoints, which used to add pause time in programs with millions of monitors.
Is ReentrantLock faster than synchronized now?
Not as a rule. Both use a CAS on the fast path and park on contention; ReentrantLock does it in Java with AbstractQueuedSynchronizer. The reasons to choose ReentrantLock are features, such as tryLock with a timeout, interruptible locking and several conditions, not raw speed, and any performance claim should be measured on your JDK.
7Conclusion
In this answer, we saw that HotSpot keeps lock state in the object's mark word, so an uncontended synchronized costs one CAS, using either legacy stack locking or the newer lightweight locking with a per-thread lock stack. Contention or wait() inflates the lock into an ObjectMonitor with an owner, an entry list and a wait set, and contending threads spin briefly before parking, which costs context switches and shows up as BLOCKED threads in dumps. Recent JDKs disabled and then removed biased locking, replaced stack locking with lightweight locking, and stopped synchronized from pinning virtual threads, with details that depend on the version.
Open with
At a high level, HotSpot keeps the lock state in the object's header, the mark word. For an uncontended lock, which is the common case, the thread does a single compare-and-swap.
Then, if they want more
In the old scheme, called stack locking, it pointed the header at a lock record on its stack; in the newer lightweight locking it sets bits in the header and pushes the object on a small per-thread lock stack. No OS call is involved, and recursive entries are tracked on the thread side.
When a second thread contends, or when the owner calls wait, the lock is inflated to an ObjectMonitor, a JVM structure with the owner, a recursion count, an entry list and a wait set.
A contending thread first spins for a short adaptive time, hoping the owner releases quickly, and then parks, which means context switches and is why heavy contention hurts so much; in a thread dump you see many BLOCKED threads waiting to lock one address.
In recent JDKs, biased locking was disabled by default in 15 and removed afterwards, lightweight locking replaced stack locking as the default over the 21 to 24 releases, and since JDK 24 synchronized no longer pins virtual threads. The exact defaults depend on the version, so I would check the release notes for the JDK we run.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 15, Java 18, Java 21, Java 24
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does HotSpot implement synchronized at a high level, from an uncontended lock to a...": https://scrivio-interview-library.netlify.app/java/synchronization/dd675a2091/