"Explain the synchronized lock upgrade process. What are biased, lightweight, and heavyweight locks?"
synchronized in modern JVM is way more sophisticated than the "heavy" reputation from Java 5. Interviewers use this to probe JVM internals — the object header Mark Word and lock upgrade path separate surface-level candidates from those who have read Java Concurrency in Practice or JVM source.
Object header Mark Word stores lock state — 2 bits for lock type plus payload (thread ID for biased, lock record pointer for lightweight, monitor pointer for heavyweight).
Biased lock: no contention. First thread to synchronize CAS-records its thread ID into Mark Word. Subsequent acquires by the same thread are nearly free — no CAS, just check the bias.
Lightweight lock: low contention. When a second thread arrives, bias revokes; each thread copies Mark Word to its stack and CAS-installs a pointer back. Brief spinning, no OS-level park.
Heavyweight lock: high contention or long waits. Lock inflates to an ObjectMonitor — threads park via OS mutex. Expensive but scales.
Upgrade is one-way: biased → lightweight → heavyweight, never downgrades. JDK 15+ deprecated biased locking (removed default), because in modern server apps the bias revocation cost outweighed the savings.
synchronized in the modern JVM is three locks in a trench coat, upgrading based on contention. The state lives in the object's Mark Word — two bits for lock type plus payload. No contention path — biased locking. The first thread to enter a synchronized block CAS-writes its thread ID into the Mark Word. Subsequent entries by the same thread just check the thread ID and go — no CAS, virtually free. This was added to optimize the common case where a single thread dominantly accesses an object. Low-contention path — lightweight locking. When a second thread arrives, the biased lock has to be revoked (which requires stopping the biased thread at a safepoint, expensive). The lock becomes lightweight: each contending thread copies the Mark Word into its own stack as a "displaced header" and CAS-installs a pointer to its stack slot into the Mark Word. If CAS fails, the thread spins briefly (adaptive spinning) waiting. No OS-level park yet. Heavy-contention path — heavyweight locking. If spinning doesn't resolve quickly, the lightweight lock inflates to a full ObjectMonitor, an OS-backed mutex. Waiting threads call pthread_mutex_lock or its equivalent and are parked by the kernel. Expensive to enter/exit but scales to many waiters. The upgrade is one-way: no downgrade. Once inflated to heavyweight, the object stays heavyweight. JDK 15 deprecated biased locking by default because modern server apps — heavily multi-threaded, many short critical sections — tended to trigger biased-lock revocation so often that the supposed optimization was a pessimization. JDK 18 removed support entirely. In practice this means for new code on current JVMs, synchronized starts as lightweight and upgrades to heavyweight under contention. The performance gap between synchronized and ReentrantLock has narrowed so much that in most cases they're interchangeable; ReentrantLock still wins for tryLock, fair locks, and condition variables.
The Mark Word + lock-type-bits detail is the "have you read JVM internals" signal.
Mention JDK 15 biased-lock deprecation — older answers that pitch "biased lock is great" are out of date.
Know that upgrade is one-way — interviewers sometimes ask "can heavyweight go back to lightweight?" to check.
If you blank on ObjectMonitor during the interview, HireMe AI can surface the term.
Yes. Each acquire increments a counter in the Mark Word (biased / lightweight) or ObjectMonitor (heavyweight); each exit decrements; release happens at 0.
When another thread tries to acquire. Revocation happens at a safepoint — all threads pause briefly — making biased locks expensive in contended workloads.
Modern server workloads almost always have contention, so revocation costs dominated. Lightweight lock is already cheap enough that biased locking wasn't a net win.
HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.
Try HireMe AI Free