关于内置锁的可重入性

 

《java并发编程实践》一书中,关于synchronized内置锁的可重入的例子如下:

------------------------------------------------------------------------------------------------------

public class Widget {

    public synchronized void doSomething() {

        ...

    }

}

 

public class LoggingWidget extends Widget {

    public synchronized void doSomething() {

        System.out.println(toString() + ": calling doSomething");

        super.doSomething();//若内置锁是不可重入的,则发生死锁

    }

}

-------------------------------------------------------------------------------------------------------

小弟在此次有两点疑惑:

1、LoggingWidget 的对象调用doSomething方法时,锁对象为LoggingWidget对象

     super.doSomething()调用是锁对象是谁?LoggingWidget对象 还是 Widget 的Class对象?

 

     运行程序,查看thread dump发现:调用super.doSomething()时锁对象依然是LoggingWidget对象

 

     "线程#1" prio=6 tid=0x0bd60400 nid=0x16f8 waiting on condition [0x0bf8f000..0x0bf8fd68]

   java.lang.Thread.State: TIMED_WAITING (sleeping)

at java.lang.Thread.sleep(Native Method)

at Widget.doSomething(Widget.java:4)

- locked <0x03fbc150> (a LoggingWidget)

at LoggingWidget.doSomething(LoggingWidget.java:5)

- locked <0x03fbc150> (a LoggingWidget)

at LoggingWidget$1.run(LoggingWidget.java:15)

 

   Locked ownable synchronizers:

- None

 

2、在网上搜索发现,大伙都如是说:

 

关于内置锁的可重入性_第1张图片


请大家讨论一下,在子类调用父类方法时,是否同时获取父类的锁(父类的class对象?)

你可能感兴趣的:(锁)