package com.neutron.t08;
import java.util.concurrent.TimeUnit;
/**
* 问题:子类的同步方法是否可以调用父类的同步方法
*/
public class T08 {
protected synchronized void hello() {
System.out.println("parent hello begin");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("parent hello end");
}
/**
运行结果:
child begin
parent hello begin
parent hello end
child end
*/
public static void main(String[] args) {
new T08Child().hello();
}
}
class T08Child extends T08 {
/**
* 允许在子类中调用父类的同步方法
*/
protected synchronized void hello() {
System.out.println("child begin");
super.hello();
System.out.println("child end");
}
}
thread08 - 子类中调用父类的同步方法
最新推荐文章于 2022-09-09 14:46:54 发布