@Override public void release() { if (lock != null) lock.close(); }
/** * Locks key. This will block until the key is acquired or the timeout is reached. * Users of this <b>must</b> close any lock acquired. * * @param key the key to lock * @return the acquired lock * @throws UncheckedTimeoutException if the lock could not be acquired within the timeout */ public Lock lock(TYPE key, long timeout, TimeUnit timeoutUnit) { try { ReentrantLock lock = locks.computeIfAbsent(key, k -> new ReentrantLock(true)); boolean acquired = lock.tryLock(timeout, timeoutUnit); if ( ! acquired) throw new UncheckedTimeoutException("Timed out waiting for the lock to " + key); return new Lock(lock); } catch (InterruptedException e) { throw new RuntimeException("Interrupted while waiting for lock of " + key); } }