简体   繁体   中英

synchronized method in derived class

如果基类A有一个“public synchronized void method(){}”,它没有被派生类B覆盖,那么将使用的是什么(即它将是派生类对象还是基类对象)访问B类中的synchronized方法?

There is no "base class object".

synchronized methods lock on the instance they're called on.

public synchronized void method() {  
    ...
};

is just the same as

public void method() {  
    synchronized(this){
        ...
    }
};

And for a super-method this means an object of class B . So the lock will be on an instance of the object B .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM