As the label space indicates, the safest option is likely to do your operations synchronized
, rather than relying on all the callers to do it right, for example:
public class Element {
public synchronized void O1() { ... }
public synchronized void O2() { ... }
public synchronized void O3() { ... }
}
This ensures that for a given instance, Element
only one operation is called at a time. Then, your subscribers do not need to worry about explicitly synchronizing anything:
for(Element element : elements) {
element.O1();
element.O2();
element.O3();
}
, , (O1, O2 O3) , . Element
, , , , :
public class Element {
public synchronized void doAllOperations() { O1(); O2(); O3(); }
}
, doAllOperations()
.