Pravat Panda
Pravat Panda

Reputation: 1090

Synchronized Overridden methods: Intrinsic Locks acquiring order for parent and child classes

Which locks are held in overridden non-static synchronized methods. Please provide the sequence in which the monitor locks of base-class and sub-class are acquired and released so that it explains the benefit of Reentrancy in java. It would be great if the sequences can be explained with the help of owning thread and acquisition count that JVM maintains to implement reentrancy.

public class Widget {
    public synchronized void doSomething() {
    }
}

public class LoggingWidget extends Widget {
    public synchronized void doSomething() {
        super.doSomething();
    }
}

Please note that this question is specifically asked for the explanation of implicit reentrant locks, so pls dont mark it as a duplicate.

Upvotes: 1

Views: 561

Answers (1)

JB Nizet
JB Nizet

Reputation: 692231

Each object has an associated monitor. When a synchronized instance method is called on an object, the monitor associated with this object needs to be held by the calling thread.

The class of the object is irrelevant.

Upvotes: 1

Related Questions