I have class A and B which inherit from Base.
Base has a private method. It is possible that A and B execute this method at the same time.
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) ? 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
incrementis atomic and ensure visibility. In that simple example, makinggetNewCountersynchronized would be enough. Using a lock object would work too.