Code example for ConcurrentMap

Methods: getputIfAbsent

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;
    } 
 
} 
Stop searching for code, let great code find you!  Add Codota to your java IDE