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

后端 未结 7 840
闹比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:43

    Yes, synchronized is equivalent to synchronized(this).

    To be more precise:

    For a class (static) method, the lock associated with the Class object for the method's class is used. For an instance method, the lock associated with this (the object for which the method was invoked) is used.

提交回复
热议问题