Backend Development 39 min read

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.

Youzan Coder
Youzan Coder
Youzan Coder
Deep Dive into Java Thread States and Synchronized Lock Implementation

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.

JavalockingsynchronizedJVM internalsThread States
Youzan Coder
Written by

Youzan Coder

Official Youzan tech channel, delivering technical insights and occasional daily updates from the Youzan tech team.

0 followers
Reader feedback

How this landed with the community

login 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.