Deep Dive into Java Thread States and Synchronized Lock Implementation
The article thoroughly explains Java thread states and how the synchronized keyword is implemented, detailing the mapping to OS thread states, the object header’s Mark Word, biased, lightweight and heavyweight locking, CAS‑based state transitions, bytecode monitor instructions, lock upgrades, and optimal usage scenarios.
This article provides an in-depth analysis of Java thread states and the internal implementation of the synchronized keyword.
It begins with the relationship between OS thread states and Java thread states, then dives into the synchronized lock mechanism, covering biased locking, lightweight locking, and heavyweight locking.
The explanation includes the role of the object header's Mark Word, the use of CAS operations, and the transition between lock states based on contention and epoch.
Example synchronized methods:
public synchronized void test1(){} public void test2(){ synchronized(new Test()){}} public static synchronized void test3(){}Detailed bytecode-level analysis of monitorenter and monitorexit is presented, showing how locks are acquired, re-entered, released, and inflated. The article also discusses thread implementation models (kernel-level, user-level, hybrid) and the mapping of Java thread states to OS states, clarifying concepts like BLOCKED, WAITING, and TIMED_WAITING. Finally, it summarizes the lock upgrade process and provides notes on when each lock type is beneficial.
Youzan Coder
Official Youzan tech channel, delivering technical insights and occasional daily updates from the Youzan tech team.
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.