I have a method
public synchronized x() {
y();
}
private y() { }
y() is only called by x(). So my question is y() has to be also a synchronized method? Or is it just implicit?
I have a method
public synchronized x() {
y();
}
private y() { }
y() is only called by x(). So my question is y() has to be also a synchronized method? Or is it just implicit?
There is no need to synchronize y
if you are sure that y is only called by x
. That would be equivalent with placing the code of y
, straight into x
.
If y() is called only from x() function then y need not be again made synchronized. However note that y() should not be static. Your solutions stays good till both x() and y() are instance methods or both are static methods.
You might want to get familiar with the concept of monitors. In your case an accessing thread to x()
will own the monitor for that Object. Any other thread which also wants to own that monitor is blocked. I.e. another thread can call y()
as it is not synchronized, another thread is blocked if it wants to get the ownership of that monitor (e.g. call x()
). The monitor will be released implicitly when the thread returns from x()
.