Code example for ConcurrentMap

Methods: get, putIfAbsent

0
public class SimpleLockManager implements LockManager { 
 
    private ConcurrentMap<String, Lock> locks = new ConcurrentHashMap<String, Lock>();
 
    public Lock getLock(String id) {
        Lock lock = locks.get(id);
        if (lock == null) {
            lock = new SimpleLock();
            Lock oldLock = locks.putIfAbsent(id, lock);
            if (oldLock != null) {
                lock = oldLock;
            } 
        } 
        return lock;
    } 
 
}