简体   繁体   English

Java同步超类的私有方法

[英]Java synchronization private method of superclass

I have class A and B which inherit from Base. 我有从A继承的A和B类。

Base has a private method. Base有一个私有方法。 It is possible that A and B execute this method at the same time. A和B可能同时执行此方法。

So I want to synchronize that method. 所以我想同步那个方法。 If I just put "synchronize" keyword in the signature, will that make that A and B execution is synchronized (share the same lock) ? 如果我只是在签名中放入“synchronize”关键字,是否会使A和B执行同步(共享同一个锁)? Or do I need to generate a static lock object...? 或者我需要生成一个静态锁定对象......?

Sorry, a bit confused... 对不起,有点困惑......

If your code looks a bit like the example below, then it is not thread safe and you need to include some form of synchronization to make sure the call to the private method increment is atomic and ensure visibility. 如果您的代码看起来有点像下面的示例,那么它不是线程安全的,您需要包含某种形式的同步,以确保对私有方法increment的调用是原子的并确保可见性。 In that simple example, making getNewCounter synchronized would be enough. 在这个简单的例子中,使getNewCounter同步就足够了。 Using a lock object would work too. 使用锁定对象也可以。

public class Base {
    private int counter;

    private void increment() {
        counter++;
    }
    protected int getNewCounter() {
        increment();
        return counter;
    }
}

public class A extends Base {
    public int aMethod() {
        return getNewCounter();
    }
}

public class B extends Base {
    public int anotherMethod() {
        return getNewCounter();
    }
}

Base has a private method. Base有一个私有方法。 It is possible that A and B execute this method at the same time. A和B可能同时执行此方法。

Private methods or fields are not inherited by derived class, so answer to your question is no. 私有方法或字段不是由派生类继承的,所以回答你的问题是否定的。

Edit As per comment: 根据评论编辑:

If you have two objects A and B and they are trying to manipulate Singleton object C, in that case it is C which should have synchronization and not A and B. 如果你有两个对象A和B并且他们试图操纵Singleton对象C,那么它就是C,它应该具有同步而不是A和B.

General rule followed in multi-threaded application is, Every class which needs protection from concurrent access in order to maintain a stable state is responsible for using synchronization correctly, you shouldn't and cant expect caller of the method to access the object state in a synchronized way. 在多线程应用程序中遵循的一般规则是,为了保持稳定状态需要保护以防止并发访问的每个类负责正确使用同步,您不应该并且不能期望该方法的调用者访问对象状态。同步方式。

What I can understand from your post is that you have a base class with a private method. 我可以从你的帖子中了解到你有一个带私有方法的基类。 You are creating different objects of this base class or the classes derived from the base class. 您正在创建此基类的不同对象或从基类派生的类。

Now if you make the private method syncronized It will not make the execution of that method syncronized, as you have multiple objects. 现在,如果你使私有方法同步它不会使该方法的执行同步,因为你有多个对象。 Instance level lock is only per object. 实例级别锁定仅适用于每个对象。

For eg You have ObjectA and ObjectB of class Test, both these objects can execute private method say myMethod() defined in class Test simultaneously even if its synchronized because the lock is on the object. 例如,你有类Test的ObjectA和ObjectB,这两个对象都可以同时执行私有方法,例如在类Test中定义的myMethod(),即使它是同步的,因为锁在对象上。

Hope this helps. 希望这可以帮助。

What i know is that, private members are NOT inherited. 我所知道的是,私人成员不是继承的。 So class A and class B cant access this private method with either using inheritance or creating an object of Base class to call this method, until there is a public method in Base class which has the access to this private method. 因此,类A和类B无法使用继承或创建Base类的对象来调用此方法来访问此私有方法,直到Base类中有一个公共方法可以访问此私有方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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