@Test public void testLocker(){ KeyLocker<String> locker = new KeyLocker(); ReentrantLock lock1 = locker.acquireLock("l1"); Assert.assertTrue(lock1.isHeldByCurrentThread()); ReentrantLock lock2 = locker.acquireLock("l2"); Assert.assertTrue(lock2.isHeldByCurrentThread()); Assert.assertTrue(lock1 != lock2); ReentrantLock lock20 = locker.acquireLock("l2"); Assert.assertTrue(lock20 == lock2); Assert.assertTrue(lock2.isHeldByCurrentThread()); Assert.assertTrue(lock20.isHeldByCurrentThread()); lock20.unlock(); Assert.assertTrue(lock20.isHeldByCurrentThread()); lock2.unlock(); Assert.assertFalse(lock20.isHeldByCurrentThread()); ReentrantLock lock200 = locker.acquireLock("l2"); Assert.assertTrue(lock2 != lock200); lock200.unlock(); Assert.assertFalse(lock200.isHeldByCurrentThread()); Assert.assertTrue(lock1.isHeldByCurrentThread()); lock1.unlock(); Assert.assertFalse(lock1.isHeldByCurrentThread()); }
public ReentrantLock acquireLock(K key) { if (key == null) throw new IllegalArgumentException("key must not be null"); Pair<KeyLock<K>, AtomicInteger> lock; synchronized (this) { lock = locks.get(key); if (lock == null) { lock = new Pair<KeyLock<K>, AtomicInteger>( new KeyLock<K>(this, key), new AtomicInteger(1)); locks.put(key, lock); } else { lock.getSecond().incrementAndGet(); } } lock.getFirst().lock(); return lock.getFirst(); }
KeyLocker { public ReentrantLock acquireLock(K key) { if (key == null) throw new IllegalArgumentException("key must not be null"); Pair<KeyLock<K>, AtomicInteger> lock; synchronized (this) { lock = locks.get(key); if (lock == null) { lock = new Pair<KeyLock<K>, AtomicInteger>( new KeyLock<K>(this, key), new AtomicInteger(1)); locks.put(key, lock); } else { lock.getSecond().incrementAndGet(); } } lock.getFirst().lock(); return lock.getFirst(); } }
KeyLocker { public ReentrantLock acquireLock(K key) { if (key == null) throw new IllegalArgumentException("key must not be null"); Pair<KeyLock<K>, AtomicInteger> lock; synchronized (this) { lock = locks.get(key); if (lock == null) { lock = new Pair<KeyLock<K>, AtomicInteger>( new KeyLock<K>(this, key), new AtomicInteger(1)); locks.put(key, lock); } else { lock.getSecond().incrementAndGet(); } } lock.getFirst().lock(); return lock.getFirst(); }  }
KeyLocker { public ReentrantLock acquireLock(K key) { if (key == null) throw new IllegalArgumentException("key must not be null"); Pair<KeyLock<K>, AtomicInteger> lock; synchronized (this) { lock = locks.get(key); if (lock == null) { lock = new Pair<KeyLock<K>, AtomicInteger>( new KeyLock<K>(this, key), new AtomicInteger(1)); locks.put(key, lock); } else { lock.getSecond().incrementAndGet(); } } lock.getFirst().lock(); return lock.getFirst(); }  ReentrantLock acquireLock(K key); Map<K, Lock> acquireLocks(final Set<K> keys); }
KeyLocker { public ReentrantLock acquireLock(K key) { if (key == null) throw new IllegalArgumentException("key must not be null"); Pair<KeyLock<K>, AtomicInteger> lock; synchronized (this) { lock = locks.get(key); if (lock == null) { lock = new Pair<KeyLock<K>, AtomicInteger>( new KeyLock<K>(this, key), new AtomicInteger(1)); locks.put(key, lock); } else { lock.getSecond().incrementAndGet(); } } lock.getFirst().lock(); return lock.getFirst(); }  ReentrantLock acquireLock(K key); Map<K, Lock> acquireLocks(final Set<K> keys);  }