thread08

来源:互联网 发布:黑马28期java就业班 编辑:程序博客网 时间:2024/06/02 03:29
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");    }}