HexplainedJavaThread dumpsHeap dumpsAbout
ViewerFormatWhat javac doesOpcodesConstantsAttributesVersionsJAR
Java class files › What javac does › synchronized

What javac does

synchronized

monitorenter and monitorexit for a block; only a flag for a method.

A synchronized block compiles to monitorenter and monitorexit around its body, with a handler that unlocks the monitor if the body throws. A synchronized method compiles to nothing at all: the ACC_SYNCHRONIZED flag tells the JVM to take the lock on entry and release it on every exit.

The source

// synchronized: monitorenter and monitorexit for a block, only a flag for a method.
public class Sync {
    private final Object lock = new Object();
    private int count;

    void increment() {
        synchronized (lock) {
            count++;
        }
    }

    synchronized int get() {
        return count;
    }
}

Compiled by javac 25 with -g and --release 8 to 25. Where releases produce different bytecode, each version is shown.

What to look for

  • The lock object is stored in a hidden local (astore_1), so the same object is unlocked even if the field is changed inside the block.
  • There are two monitorexits: one on the normal path, and one in the handler, which then rethrows.
  • The handler's range also covers itself (the second line of the exception table), so the monitor is still released if something fails while unlocking.
  • get() contains no monitor instructions: it is the synchronized flag on the method that does the locking.

Sync.class

public Sync()

javac --release 8 to 25 (the same bytecode for every release)

2public class Sync {
0aload_0 // this
1invokespecial #1// new Object()
3private final Object lock = new Object();
4aload_0 // this
5new #2// Object
8dup
9invokespecial #1// new Object()
12putfield #7// Sync.lock : Object
15return

Open in the viewer

void increment()

javac --release 8 to 25 (the same bytecode for every release)

7synchronized (lock) {
0aload_0 // this
1getfield #7// Sync.lock : Object
4dup
5astore_1
6monitorenter
8count++;
7aload_0 // this
8dup
9getfield #13// Sync.count : int
12iconst_1
13iadd
14putfield #13// Sync.count : int
9}
17aload_1
18monitorexit
19goto 27
22astore_2 catch any, any
23aload_1
24monitorexit
25aload_2
26athrow
10}
27return
FromToHandlerCatches
71922anything (any)
222522anything (any)

Open in the viewer

synchronized int get()

javac --release 8 to 25 (the same bytecode for every release)

13return count;
0aload_0 // this
1getfield #13// Sync.count : int
4ireturn

Open in the viewer

← finallyAll topicsInner classes and nestmates →

By the author of this site

Reading bytecode is half the story; running it is the other half. You execute exactly this kind of code in Build a JVM in Rust: Class files, an interpreter, objects and invokedynamic from scratch, on the real JDK class library.

Read Chapter 1 free

Java and the JVM are trademarks of Oracle; this site is not affiliated with Oracle. Field names follow the Java Virtual Machine Specification; the explanations are our own.

Sister sites: What's in my DB · LLM Anatomy · Distribution Atlas · Notation Decoder · uresk.net

Made by Spencer Uresk · About · Files you open stay on your device.