I have a class ClassA
that has a public void doSomething()
method. This ClassA
is widely used in different applications; some require synchronized
and others do not. It is also the case that I do not want the (tiny, but non-zero) performance hit associated with calling a synchronized
method when I do not need this.
Suppose that ClassB
will call doSomething()
and requires that this is synchronized
, yet ClassC
does not require this. What are the ways that I can achieve this in the design of my program?
Is it sufficient to have the method in ClassB
:
private synchronized void doSomething() {
this.classAInstance.doSomething();
}
thus avoiding the need to specify ClassC
's doSomething()
as synchronized
?