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

后端 未结 7 832
闹比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:46

    Just a small addition for people who might be interested in the future..

    Additionally remember that locks in Java are reentrant. If they were not this code of yours would result in a deadlock since as you've indicated both operations require the same lock.

提交回复
热议问题