public void unlock(int id) {
      lock.lock();
      try {
        boolean actuallyRemoved = lockedIds.remove(id);
        if (actuallyRemoved) {
          //This wakes up all threads waiting -- i.e. lockedIds.size() -- we assume it is usually just a few of them.
          // But there could be pathological scenarios there a lot of threads waiting: and each thread will need to
          // re-acquire lock, check lockedIds.contains() -- and all threads but one return to waiting after the check.
          unlockCondition.signalAll();
        }
        else {
          throw new IllegalStateException("Trying to unlock(" + id + ") which is not currently locked " + lockedIds);
        }
      }
      finally {
        lock.unlock();
      }
    }
public void unlock(int id)