}
public boolean acquireLock(Object key, long timeout, TimeUnit unit) throws InterruptedException {
while (true) {
Lock lock = getLock(key);
if (lock.tryLock(timeout, unit)) {
// lock acquired. Now check if it is the *correct* lock!
Lock existingLock = locks.putIfAbsent(key, lock);
if (existingLock != null && existingLock != lock) {
// we have the wrong lock! Unlock and retry.
lock.unlock();