Backend Development 17 min read

Deep Dive into Java synchronized: Usage, Lock Mechanisms, and JVM Implementation

This article explains the Java synchronized keyword, covering its basic usage on methods and code blocks, the underlying monitor mechanism, object header layout, lock states such as biased, lightweight, and heavyweight locks, and provides detailed code examples and JVM internal implementations.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Deep Dive into Java synchronized: Usage, Lock Mechanisms, and JVM Implementation

The synchronized keyword is a fundamental part of Java concurrency, providing mutual exclusion and exclusive access to shared resources. When a thread acquires a lock on a resource, other threads must wait until the lock is released.

Overview of synchronized

synchronized is a built‑in synchronization mechanism in Java. It enforces mutual exclusion and exclusive semantics, ensuring that only one thread can execute a synchronized block or method at a time.

Usage of synchronized

Instance method : Locks the object instance. Example: public synchronized void method() { // ... } A complete example demonstrates a class implementing Runnable with a synchronized instance method increase() that increments a static counter. public class TSynchronized implements Runnable { static int i = 0; public synchronized void increase() { i++; System.out.println(Thread.currentThread().getName()); } @Override public void run() { for (int j = 0; j < 1000; j++) { increase(); } } public static void main(String[] args) throws InterruptedException { TSynchronized t = new TSynchronized(); Thread a = new Thread(t); Thread b = new Thread(t); a.start(); b.start(); a.join(); b.join(); System.out.println("i = " + i); } } The program prints i = 2000 , showing the exclusive nature of the lock.

Static method : Locks the Class object. Example: public static synchronized void increase() { } This ensures that all threads contend for the same class‑level lock.

Code block : Locks an arbitrary object. Example: public void run() { synchronized(obj) { for (int j = 0; j < 1000; j++) { i++; } } } The lock can be on this , a class object, or any user‑defined object.

Underlying principles

At the JVM level, a synchronized block is implemented with the bytecode instructions monitorenter and monitorexit . The underlying monitor object is the basic unit of synchronization.

Object header layout

Each Java object consists of three parts: the object header, instance data, and padding. The header contains the MarkWord and a Klass pointer. The MarkWord stores lock information, hash code, GC flags, and other metadata.

Lock states encoded in the MarkWord include:

No‑lock (unbiased)

Biased lock

Lightweight lock

Heavyweight lock

GC mark

Lock upgrade process

When contention increases, the JVM upgrades the lock state:

No‑lock → Biased lock (optimised for single‑thread ownership)

Biased lock → Lightweight lock (uses CAS and spin‑waiting)

Lightweight lock → Heavyweight lock (uses OS mutexes and may block threads)

Each upgrade involves copying the MarkWord to a lock record on the thread stack and updating pointers. If a lock cannot be acquired after several spins, it escalates to a heavyweight lock.

Synchronized implementation details

For synchronized code blocks, the compiled bytecode contains monitorenter at the block start and one or more monitorexit instructions at each exit point. The exception table ensures the monitor is released even when an exception occurs.

For synchronized methods, the JVM sets the ACC_SYNCHRONIZED flag on the method. The interpreter or JIT recognises this flag and performs the necessary monitor acquisition without explicit monitorenter / monitorexit instructions.

Conclusion

The synchronized keyword provides a high‑level abstraction for mutual exclusion in Java. Understanding its usage, the object header structure, lock states, and the JVM’s monitor implementation helps developers write efficient concurrent code and diagnose performance issues.

JavaJVMConcurrencylockingsynchronizedMonitorobject header
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

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.