net.anotheria.util.concurrency.SafeIdBasedLockManager Maven / Gradle / Ivy
package net.anotheria.util.concurrency;
import java.util.HashMap;
import java.util.Map;
/**
* SafeIdBasedLockManager class.
*
* @author another
* @version $Id: $Id
*/
public class SafeIdBasedLockManager extends AbstractIdBasedLockManager
implements IdBasedLockManager {
/**
* Serialization version unique identifier.
*/
private static final long serialVersionUID = -7086955847344168761L;
private Map> locks = new HashMap<>();
/** {@inheritDoc} */
@Override
public IdBasedLock obtainLock(K id) {
synchronized (this) {
IdBasedLock lock = locks.get(id);
if (lock != null) {
lock.increaseRefCount();
return lock;
}
lock = new IdBasedLock<>(id, this);
locks.put(id, lock);
return lock;
}
}
/** {@inheritDoc} */
@Override
public void releaseLock(IdBasedLock lock) {
synchronized (this) {
K id = lock.getId();
if (lock.getRefCount().get() == 1) {
locks.remove(id);
}
lock.decreaseRefCount();
}
}
/** {@inheritDoc} */
@Override
protected Map> getLockMap() {
return locks;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy