/** * Return a lock for the given key. The lock is already locked. * * @param key */ public ReentrantLock acquireLock(K key) { if (key == null) throw new IllegalArgumentException("key must not be null"); lockPool.purge(); ReentrantLock lock = lockPool.get(key); lock.lock(); return lock; }
/** * Acquire locks for a set of keys. The keys will be * sorted internally to avoid possible deadlock. * * @throws ClassCastException if the given {@code keys} * contains elements that are not mutually comparable */ public Map<K, Lock> acquireLocks(Set<? extends K> keys) { Object[] keyArray = keys.toArray(); Arrays.sort(keyArray); lockPool.purge(); Map<K, Lock> locks = new LinkedHashMap<>(keyArray.length); for (Object o : keyArray) { @SuppressWarnings("unchecked") K key = (K)o; ReentrantLock lock = lockPool.get(key); locks.put(key, lock); } for (Lock lock : locks.values()) { lock.lock(); } return locks; } }
/** * Return a lock for the given key. The lock is already locked. * * @param key */ public ReentrantLock acquireLock(K key) { if (key == null) throw new IllegalArgumentException("key must not be null"); lockPool.purge(); ReentrantLock lock = lockPool.get(key); lock.lock(); return lock; }
/** * Get the ReentrantReadWriteLock corresponding to the given id * @param id an arbitrary number to identify the lock */ public ReentrantReadWriteLock getLock(long id) { lockPool.purge(); ReentrantReadWriteLock readWriteLock = lockPool.get(id); return readWriteLock; }
/** * Return a lock for the given key. The lock is already locked. * * @param key */ public ReentrantLock acquireLock(K key) { if (key == null) throw new IllegalArgumentException("key must not be null"); lockPool.purge(); ReentrantLock lock = lockPool.get(key); lock.lock(); return lock; }
/** * Return a lock for the given key. The lock is already locked. * * @param key */ public ReentrantLock acquireLock(K key) { if (key == null) throw new IllegalArgumentException("key must not be null"); lockPool.purge(); ReentrantLock lock = lockPool.get(key); lock.lock(); return lock; }
/** * Acquire locks for a set of keys. The keys will be * sorted internally to avoid possible deadlock. * * @throws ClassCastException if the given {@code keys} * contains elements that are not mutually comparable */ public Map<K, Lock> acquireLocks(Set<? extends K> keys) { Object[] keyArray = keys.toArray(); Arrays.sort(keyArray); lockPool.purge(); Map<K, Lock> locks = new LinkedHashMap<>(keyArray.length); for (Object o : keyArray) { @SuppressWarnings("unchecked") K key = (K)o; ReentrantLock lock = lockPool.get(key); locks.put(key, lock); } for (Lock lock : locks.values()) { lock.lock(); } return locks; } }
/** * Acquire locks for a set of keys. The keys will be * sorted internally to avoid possible deadlock. * * @throws ClassCastException if the given {@code keys} * contains elements that are not mutually comparable */ public Map<K, Lock> acquireLocks(Set<? extends K> keys) { Object[] keyArray = keys.toArray(); Arrays.sort(keyArray); lockPool.purge(); Map<K, Lock> locks = new LinkedHashMap<>(keyArray.length); for (Object o : keyArray) { @SuppressWarnings("unchecked") K key = (K)o; ReentrantLock lock = lockPool.get(key); locks.put(key, lock); } for (Lock lock : locks.values()) { lock.lock(); } return locks; } }
/** * Acquire locks for a set of keys. The keys will be * sorted internally to avoid possible deadlock. * * @throws ClassCastException if the given {@code keys} * contains elements that are not mutually comparable */ public Map<K, Lock> acquireLocks(Set<? extends K> keys) { Object[] keyArray = keys.toArray(); Arrays.sort(keyArray); lockPool.purge(); Map<K, Lock> locks = new LinkedHashMap<K, Lock>(keyArray.length); for (Object o : keyArray) { @SuppressWarnings("unchecked") K key = (K)o; ReentrantLock lock = lockPool.get(key); locks.put(key, lock); } for (Lock lock : locks.values()) { lock.lock(); } return locks; } }
@VisibleForTesting public void waitForWaiters(long id, int numWaiters) throws InterruptedException { for (ReentrantReadWriteLock readWriteLock;;) { readWriteLock = lockPool.get(id); if (readWriteLock != null) { synchronized (readWriteLock) { if (readWriteLock.getQueueLength() >= numWaiters) { return; } } } Thread.sleep(50); } } }