ReviseAlgo Logo

Multithreading

Static Synchronization

Understand class-level locks and the differences between static and instance synchronization.

Interview: Commonly tests the difference between instance-level locking and class-level locking in mixed contexts.

Last Updated: June 13, 2026 10 min read

In Java, static variables are shared across all instances of a class. To protect static variables from race conditions, you must use static synchronization, which locks on the Class object rather than an individual instance.

Core Idea

Static synchronized methods lock on the Class object (e.g. MyClass.class) instead of the instance ("this").

Why It Matters

If multiple threads modify a static field using instance locks, threads accessing different instances will bypass locking.

Interview Lens

Tests whether a static synchronized method blocks an instance synchronized method of the same class.

Class Lock vs. Instance Lock

A static synchronized method is equivalent to:

public static void method() {
    synchronized (MyClass.class) {
        // Critical section for static resource
    }
}

Because the class lock is completely distinct from any instance lock:

  • Thread 1 executing a static synchronized method does NOT prevent Thread 2 from entering an instance synchronized method on the same object.
  • They lock on two different monitor objects (the Class metadata object vs. the concrete instance).

Code Walkthrough

This program demonstrates that static and instance synchronization can execute in parallel because they target different locks.

public class StaticSyncDemo {
    public static synchronized void staticMethod() {
        System.out.println(Thread.currentThread().getName() + " inside staticMethod");
        try { Thread.sleep(2000); } catch (InterruptedException e) {}
        System.out.println(Thread.currentThread().getName() + " exiting staticMethod");
    }

public synchronized void instanceMethod() { System.out.println(Thread.currentThread().getName() + " inside instanceMethod"); }

public static void main(String[] args) { StaticSyncDemo demo = new StaticSyncDemo();

// Thread 1 locks on StaticSyncDemo.class new Thread(StaticSyncDemo::staticMethod, "Thread-1").start();

// Thread 2 locks on "demo" instance. Starts immediately without blocking. new Thread(demo::instanceMethod, "Thread-2").start(); } }

Interview-Relevant Information

Q: Can different ClassLoader configurations affect static synchronization?
Answer: Yes. In complex systems (like OSGi or application servers) where a class can be loaded by different ClassLoaders, multiple Class objects represent that same class in the JVM. Since each ClassLoader creates its own Class instance, static synchronization might not prevent race conditions if threads use instances loaded by different ClassLoaders.

Quick Checklist

What object does a static synchronized method lock on? Are class locks and instance locks independent? If yes, you understand static synchronization.

Use Cases

Protecting static cache metrics across all active class instances.

Safeguarding global configurations or registry collections.

Common Mistakes

Using instance synchronized methods to modify static variables, which fails to protect the variables when multiple instances are used.

Locking on static object references that can be modified, allowing threads to lock on different objects.