Static And Instance Locks Do Not Exclude

Show that an instance monitor never blocks the class monitor: code holding one object lock runs while the static critical section proceeds concurrently.

JavaResilience

Code

snippet
public class LockScopeMismatch {

    private static int sharedTotal = 0;
    private final Object instanceLock = new Object();

    void bumpWithInstanceLock() {
        synchronized (instanceLock) {            // one monitor per object instance
            sharedTotal++;
        }
    }

    static void bumpWithClassLock() {
        synchronized (LockScopeMismatch.class) { // the class monitor is a different lock
            sharedTotal++;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        LockScopeMismatch a = new LockScopeMismatch();
        LockScopeMismatch b = new LockScopeMismatch();
        a.bumpWithInstanceLock();
        b.bumpWithInstanceLock();
        bumpWithClassLock();

        Thread holder = new Thread(() -> {
            synchronized (a.instanceLock) {
                System.out.println("holder entered a's instance lock");
                sleepQuietly(300);
                System.out.println("holder still inside the critical section");
            }
        });
        holder.start();
        Thread.sleep(100); // let the holder enter first

        synchronized (b.instanceLock) {
            System.out.println("b's instance lock taken while a's is held");
        }
        synchronized (LockScopeMismatch.class) {
            System.out.println("class lock taken while a's instance lock is held");
        }
        holder.join();
        System.out.println("sharedTotal = " + sharedTotal);
    }

    private static void sleepQuietly(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

Under the hood — Static And Instance Locks Do Not Exclude

Static And Instance Locks Do Not Exclude keeps everything in the classes LockScopeMismatch, monitor and lock and the functions bumpWithClassLock and sleepQuietly.

Caveats

  • No lockfile entry, no version pinning — the file needs nothing beyond the runtime.

Applying it

Signature: static void bumpWithClassLock()

The demo output is labeled (holder entered a's instance lock, holder still inside the critical section, b's instance lock taken while a's is held); each label names the call that printed it.

The sequence bumpWithClassLock and sleepQuietly lives in main; running the file triggers it.

Statements that matter

synchronized (LockScopeMismatch.class) { // the class monitor is a different lock
    sharedTotal++;
}
synchronized (instanceLock) {            // one monitor per object instance
    sharedTotal++;
}
Thread.sleep(100); // let the holder enter first
synchronized (b.instanceLock) {
    System.out.println("b's instance lock taken while a's is held");
}

Interactive Tools for This Topic

FAQ

What does Static And Instance Locks Do Not Exclude print when run?
No setup needed: run the file and look for labeled lines like `holder entered a's instance lock`, `holder still inside the critical section`, `b's instance lock taken while a's is held` in the output.