Java: A synchronized method in the superclass acquires the same lock as one in the subclass, right?

后端 未结 7 833
闹比i
闹比i 2021-02-12 10:12
class A {
    public synchronized void myOneMethod() {
        // ...
    }
}

class B extends A {
    public synchronized void myOtherMethod() {
        // ...
    }
}
         


        
7条回答
  •  情歌与酒
    2021-02-12 10:36

    Yes you are correct

    When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object. In this case the object is B

提交回复
热议问题