Object Lock vs Class Lock in Java

Naveen AnamaneniUpdated · 4 min read

Java has two kinds of intrinsic locks. An object lock belongs to one instance: synchronized instance methods and synchronized (this) use it, and it protects that object's instance fields. A class lock belongs to the Class object: static synchronized methods and synchronized (MyClass.class) use it, and it protects static fields. Because they are different locks, a thread holding one doesn't block a thread wanting the other. In this lesson, you will see both locks in action and compare them.

Example: Which Calls Block Each Other?

Let's see an example where pairs of calls are timed. Each method holds its lock for 300 milliseconds; if two calls take about 600 milliseconds together, they blocked each other.

Locks.java▶ Try it
public class Locks {
    synchronized void instanceWork() { pause(); }
    static synchronized void staticWork() { pause(); }
 
    static void pause() {
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
 
    static String together(Runnable first, Runnable second)
            throws InterruptedException {
        long start = System.nanoTime();
        Thread a = new Thread(first);
        Thread b = new Thread(second);
        a.start();
        b.start();
        a.join();
        b.join();
        long ms = (System.nanoTime() - start) / 1_000_000;
        return ms >= 550 ? "waited (one after another)"
                         : "ran at the same time";
    }
 
    public static void main(String[] args) throws InterruptedException {
        Locks navika = new Locks();
        Locks srihitha = new Locks();
 
        System.out.println("Same object, two instance calls: "
            + together(navika::instanceWork, navika::instanceWork));
        System.out.println("Two objects, instance calls:     "
            + together(navika::instanceWork, srihitha::instanceWork));
        System.out.println("Two static calls:                "
            + together(Locks::staticWork, Locks::staticWork));
        System.out.println("Static and instance call:        "
            + together(Locks::staticWork, navika::instanceWork));
    }
}
Output
Same object, two instance calls: waited (one after another)
Two objects, instance calls:     ran at the same time
Two static calls:                waited (one after another)
Static and instance call:        ran at the same time

Explanation:

  • Two instance calls on the same object share its object lock, so the second waits.
  • Instance calls on different objects use different object locks, so they overlap.
  • Two static calls share the one class lock, so they wait.
  • A static call and an instance call use different locks (class vs object), so they overlap.

Differences Between Object Lock and Class Lock

Object lockClass lock
Belongs toOne instanceThe Class object (one per class)
Taken bysynchronized instance methods, synchronized (this) or another objectstatic synchronized methods, synchronized (X.class)
ProtectsInstance fields of that objectStatic fields
How manyOne per objectOne per class (per class loader)
Blocks the other kindNoNo

Common Mistakes

Thinking a static synchronized method blocks all instance synchronized methods. It doesn't.

Protecting shared static data with object locks. Use the class lock.

Interview Corner

What is the difference between an object lock and a class lock?

An object lock is the intrinsic lock of a particular instance, acquired by synchronized instance methods or synchronized (obj) blocks; it serializes threads using the same object and is used to protect instance state. A class lock is the intrinsic lock of the Class object, acquired by static synchronized methods or synchronized (MyClass.class); there's one per class, and it protects static state. The two are independent, so holding one doesn't block threads acquiring the other.

Practice

  1. Easy: Predict the result of two synchronized (Locks.class) blocks run by two threads, then add it to the example to check.
  2. Harder: Add an instance method that locks Locks.class instead of this, and test whether it blocks staticWork.

Quick check: Object lock vs class lock

4 questions
1How many class locks does a class have?
2Do instance calls on two different objects block each other?
3Does a static synchronized call block an instance synchronized call?
4Which lock protects static fields?

Key Takeaways

Was this tutorial helpful?

Lesson 401 of 482