验证synchronized 是可重入锁?
public
class
Xttblog
extends
SuperXttblog {
public
static
void
main(String[] args) {
Xttblog child =
new
Xttblog();
child.doSomething();
}
public
synchronized
void
doSomething() {
System.out.println(
"child.doSomething()"
+ Thread.currentThread().getName());
doAnotherThing();
// 调用自己类中其他的synchronized方法
}
private
synchronized
void
doAnotherThing() {
super
.doSomething();
// 调用父类的synchronized方法
System.out.println(
"child.doAnotherThing()"
+ Thread.currentThread().getName());
}
}
class
SuperXttblog {
public
synchronized
void
doSomething() {
System.out.println(
"father.doSomething()"
+ Thread.currentThread().getName());
}
}
执行结果:
child.doSomething()Thread-5492
father.doSomething()Thread-5492
child.doAnotherThing()Thread-5492