public class MyClass {
private HashSet mySet = new HashSet();
public synchronized void add(Object in) { // get 'this' monitor' synchronized (mySet) { // get 'mySet' monitor mySet.add(in); } }
private void doRemove(Object out) { }
public void remove(Object out) { synchronized (mySet) { // get 'mySet' monitor doRemove(out); // get 'this' monitor } } } |