private CountingLock getKeyLock(final Object key) { assert key != null : "contract broken: key != null"; getStripedLock(key).tryLock(); try { final CountingLock result; final CountingLock previousLock = key2lock.get(key); if (previousLock == null) { result = new CountingLock(lockTimeout, lockTimeoutUnit); key2lock.put(key, result); } else { result = previousLock; } result.incrementUses(); return result; } finally { getStripedLock(key).unlock(); } }
private CountingLock getKeyLock(final Object key) { assert key != null : "contract broken: key != null"; getStripedLock(key).tryLock(); try { final CountingLock result; final CountingLock previousLock = key2lock.get(key); if (previousLock == null) { result = new CountingLock(lockTimeout, lockTimeoutUnit); key2lock.put(key, result); } else { result = previousLock; } result.incrementUses(); return result; } finally { getStripedLock(key).unlock(); } }
/** * Creates a new instance of {@link StripedKeyLockManager} with the given settings. * * @param lockTimeout * the time to wait for a lock before a Exception is thrown - must be greater than 0 * @param lockTimeoutUnit * the unit for lockTimeout - must not be null * @param numberOfStripes * the number of stripes used for locking */ public StripedKeyLockManager(final long lockTimeout, final TimeUnit lockTimeoutUnit, final int numberOfStripes) { Contract.isNotNull(lockTimeoutUnit, "lockTimeoutUnit != null"); Contract.isTrue(lockTimeout > 0, "lockTimeout > 0"); Contract.isTrue(numberOfStripes > 0, "numberOfStripes > 0"); this.lockTimeout = lockTimeout; this.lockTimeoutUnit = lockTimeoutUnit; this.stripes = new CountingLock[numberOfStripes]; setAll(stripes, i -> new CountingLock(lockTimeout, lockTimeoutUnit)); }
/** * Creates a new instance of {@link StripedKeyLockManager} with the given settings. * * @param lockTimeout * the time to wait for a lock before a Exception is thrown - must be greater than 0 * @param lockTimeoutUnit * the unit for lockTimeout - must not be null * @param numberOfStripes * the number of stripes used for locking */ public StripedKeyLockManager(final long lockTimeout, final TimeUnit lockTimeoutUnit, final int numberOfStripes) { Contract.isNotNull(lockTimeoutUnit, "lockTimeoutUnit != null"); Contract.isTrue(lockTimeout > 0, "lockTimeout > 0"); Contract.isTrue(numberOfStripes > 0, "numberOfStripes > 0"); this.lockTimeout = lockTimeout; this.lockTimeoutUnit = lockTimeoutUnit; this.stripes = new CountingLock[numberOfStripes]; setAll(stripes, i -> new CountingLock(lockTimeout, lockTimeoutUnit)); }