is synchronized by class name in a function will be valid across extended classes?

爱⌒轻易说出口 提交于 2020-01-11 11:05:02

问题


I have a methode foo in base class uses Synchronized (class name) , and two classes A and B that extends the base class. if i called foo from A instance and B instance in two different thread are they gonna be Synchronized. here's a sample code :

class BaseClass { 
        void foo() {
        synchronized(BaseClass.class)   
            // do something like increment count 
        }   
    }


    class A extends BaseClass {
    }


    class B extends BaseClass {
    }

    A a = new A(); 
    B b = new B();
    //in thread 1
    a.foo() ; 
    //in thread 2
    b.foo() ;

回答1:


Yes, that will be synchronized across all instances of all classes extending BaseClass (including BaseClass itself). The BaseClass.class reference will basically be a single reference for the whole classloader. Do you really want that?

Usually, when synchronization is required, static methods should synchronize on something static, and instance methods should synchronize on something related to the instance. Personally I don't like synchronizing on either this or a Class reference - as both of those references are available elsewhere, so other code could synchronize on the same monitor, making it hard to reason about the synchronization. Instead, I would tend to have:

public class Foo {
    private final Object instanceLock = new Object();

    public void doSomething() {
        synchronized (instanceLock) {
            // Stuff
        }
    }
}

public class Bar {
    private static final Object staticLock = new Object();

    public static void doSomething() {
        synchronized (staticLock) {
            // Stuff
        }
    }
}

(I typically actually just use lock as the name; I've just made it more explicit here for clarity.)




回答2:


Yes, you are synchronizing on the same thing.




回答3:


Yes. It is synchronized, even though sequence of thread execution is not guaranteed.



来源:https://stackoverflow.com/questions/8934220/is-synchronized-by-class-name-in-a-function-will-be-valid-across-extended-classe

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