Consider the following code:
public class Foo {
private static final Object LOCK = new Object();
private Object _lockRef1 = LOCK;
private Object _lockRef2 = LOCK;
private int _indx = 0;
public void dec() {
synchronized(_lockRef1) {
_indx--;
}
}
public void inc() {
synchronized(_lockRef2) {
_indx++;
}
}
}
Is call to methods dec()
and inc()
threadsafe? On the one hand these methods are synchronized on two different instances _lockRef1
and _lockRef2
. On the other hand, these instances "point" on the same object LOCK
...