synchronized关键字

懵懂的女人 提交于 2020-01-02 21:54:50

验证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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!