Reentrant locks – Java concurrency in practice

Here are some sample code for reentry locking for "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 in the above code... "Because the dosomething methods in the widget and loggingwidget are synchronized, each method will try to obtain the lock on the widget before continuing."

I run the above code to observe the internal lock The above reference seems to imply that a thread acquires the internal lock of the widget object, but what I observe is that the thread acquires the lock on the loggingwidget I'm not sure how to verify the acquisition count, so I can't observe it

Can the book use the name loggingwidget / widget interchangeably, or should we deliberately observe the locking of widget objects?

Edit: full excerpt

Solution

Yes, the author can use loggingwidget / widget interchangeably, because according to the OOP inheritance principle, the loggingwidget object is also a widget class object In this example, only one object instance will be created and used as a synchronization monitor for re - entry

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>