Here is some sample code for reentrant locking from 'Java concurrency in practice':
class Widget {
public synchronized void doSomething() {
System.out.println(toString() + ": calling superclass doSomething");
}
}
class LoggingWidget extends Widget {
public synchronized void doSomething() {
System.out.println(toString() + ": calling subclass doSomething");
super.doSomething();
}
}
The book explains that in the above code... "Because the doSomething methods in Widget and LoggingWidget are both synchronized, each tries to acquire the lock on the Widget before proceeding."
I ran the above code to observe the intrinsic lock. The above quote seems to imply that a thread acquires an intrinsic lock on the Widget object, but what I observed was that the thread acquires a lock on LoggingWidget. I am not sure how to verify the acquisition count, so wasn't able to observe that.
Is the book using the names LoggingWidget/Widget interchangeably or should I be observing a lock on the Widget object specifically?
Edit: Full Excerpt
Reentrancy facilitates encapsulation of locking behavior, and thus simplifies the development of object-oriented concurrent code. Without reentrant locks, the very natural-looking code in Listing 2.7 , in which a subclass overrides a synchronized method and then calls the superclass method, would deadlock. Because the doSomething methods in Widget and LoggingWidget are both synchronized, each tries to acquire the lock on the Widget before proceeding. But if intrinsic locks were not reentrant, the call to super.doSomething would never be able to acquire the lock because it would be considered already held, and the thread would permanently stall waiting for a lock it can never acquire. Reentrancy saves us from deadlock in situations like this.