/** * Returns a new {@link KeyLockManager} with default settings. The best available multi purpose implementation is * used. * * @return the newly created lock */ public static KeyLockManager newLock() { return new StripedKeyLockManager(DEFAULT_LOCK_TIMEOUT, TimeUnit.HOURS); }
private <R> R executeLockedInternal(final Object key, final ReturnValueLockCallback<R> callback) { assert key != null : "contract broken: key != null"; assert callback != null : "contract broken: callback != null"; final CountingLock lock = getKeyLock(key); try { lock.tryLock(); try { return callback.doInLock(); } finally { lock.unlock(); } } finally { freeKeyLock(key, lock); } }
@Override public final <R> R executeLocked(final Object key, final ReturnValueLockCallback<R> callback) { Contract.isNotNull(key, "key != null"); Contract.isNotNull(callback, "callback != null"); return executeLockedInternal(key, callback); }
private void freeKeyLock(final Object key, final CountingLock lock) { assert key != null : "contract broken: key != null"; assert lock != null : "contract broken: lock != null"; getStripedLock(key).tryLock(); try { lock.decrementUses(); if (!lock.isUsed()) { key2lock.remove(key); } } finally { getStripedLock(key).unlock(); } }
private <R> R executeLockedInternal(final Object key, final ReturnValueLockCallback<R> callback) { assert key != null : "contract broken: key != null"; assert callback != null : "contract broken: callback != null"; final CountingLock lock = getKeyLock(key); try { lock.tryLock(); try { return callback.doInLock(); } finally { lock.unlock(); } } finally { freeKeyLock(key, lock); } }
@Override public final <R> R executeLocked(final Object key, final ReturnValueLockCallback<R> callback) { Contract.isNotNull(key, "key != null"); Contract.isNotNull(callback, "callback != null"); return executeLockedInternal(key, callback); }
private void freeKeyLock(final Object key, final CountingLock lock) { assert key != null : "contract broken: key != null"; assert lock != null : "contract broken: lock != null"; getStripedLock(key).tryLock(); try { lock.decrementUses(); if (!lock.isUsed()) { key2lock.remove(key); } } finally { getStripedLock(key).unlock(); } }
@Override public final void executeLocked(final Object key, final LockCallback callback) { Contract.isNotNull(key, "key != null"); Contract.isNotNull(callback, "callback != null"); executeLockedInternal(key, () -> { callback.doInLock(); return null; }); }
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(); } }
/** * Returns a new {@link KeyLockManager} with default settings. The best available multi purpose implementation is * used. * * @return the newly created lock */ public static KeyLockManager newLock() { return new StripedKeyLockManager(DEFAULT_LOCK_TIMEOUT, TimeUnit.HOURS); }
@Override public final void executeLocked(final Object key, final LockCallback callback) { Contract.isNotNull(key, "key != null"); Contract.isNotNull(callback, "callback != null"); executeLockedInternal(key, () -> { callback.doInLock(); return null; }); }
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(); } }
/** * Returns a new {@link KeyLockManager} with the given timeout settings. The best available multi purpose * implementation is used. * * @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 * * @return the newly created lock */ public static KeyLockManager newLock(final long lockTimeout, final TimeUnit lockTimeoutUnit) { return new StripedKeyLockManager(lockTimeout, lockTimeoutUnit); }
/** * Returns a new {@link KeyLockManager} with the given timeout settings. The best available multi purpose * implementation is used. * * @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 * * @return the newly created lock */ public static KeyLockManager newLock(final long lockTimeout, final TimeUnit lockTimeoutUnit) { return new StripedKeyLockManager(lockTimeout, lockTimeoutUnit); }