void acquire(String safeKey) { WriteLock writeLock; synchronized (this) { writeLock = locks.get(safeKey); if (writeLock == null) { writeLock = writeLockPool.obtain(); locks.put(safeKey, writeLock); } writeLock.interestedThreads++; } writeLock.lock.lock(); }
void release(String safeKey) { WriteLock writeLock; synchronized (this) { writeLock = Preconditions.checkNotNull(locks.get(safeKey)); if (writeLock.interestedThreads < 1) { throw new IllegalStateException("Cannot release a lock that is not held" + ", safeKey: " + safeKey + ", interestedThreads: " + writeLock.interestedThreads); } writeLock.interestedThreads--; if (writeLock.interestedThreads == 0) { WriteLock removed = locks.remove(safeKey); if (!removed.equals(writeLock)) { throw new IllegalStateException("Removed the wrong lock" + ", expected to remove: " + writeLock + ", but actually removed: " + removed + ", safeKey: " + safeKey); } writeLockPool.offer(removed); } } writeLock.lock.unlock(); }
void release(String safeKey) { WriteLock writeLock; synchronized (this) { writeLock = Preconditions.checkNotNull(locks.get(safeKey)); if (writeLock.interestedThreads < 1) { throw new IllegalStateException("Cannot release a lock that is not held" + ", safeKey: " + safeKey + ", interestedThreads: " + writeLock.interestedThreads); } writeLock.interestedThreads--; if (writeLock.interestedThreads == 0) { WriteLock removed = locks.remove(safeKey); if (!removed.equals(writeLock)) { throw new IllegalStateException("Removed the wrong lock" + ", expected to remove: " + writeLock + ", but actually removed: " + removed + ", safeKey: " + safeKey); } writeLockPool.offer(removed); } } writeLock.lock.unlock(); }
void acquire(String safeKey) { WriteLock writeLock; synchronized (this) { writeLock = locks.get(safeKey); if (writeLock == null) { writeLock = writeLockPool.obtain(); locks.put(safeKey, writeLock); } writeLock.interestedThreads++; } writeLock.lock.lock(); }
void release(Key key) { WriteLock writeLock; synchronized (this) { writeLock = locks.get(key); if (writeLock == null || writeLock.interestedThreads <= 0) { throw new IllegalArgumentException( "Cannot release a lock that is not held" + ", key: " + key + ", interestedThreads: " + (writeLock == null ? 0 : writeLock.interestedThreads)); } if (--writeLock.interestedThreads == 0) { WriteLock removed = locks.remove(key); if (!removed.equals(writeLock)) { throw new IllegalStateException("Removed the wrong lock" + ", expected to remove: " + writeLock + ", but actually removed: " + removed + ", key: " + key); } writeLockPool.offer(removed); } } writeLock.lock.unlock(); }
void acquire(Key key) { WriteLock writeLock; synchronized (this) { writeLock = locks.get(key); if (writeLock == null) { writeLock = writeLockPool.obtain(); locks.put(key, writeLock); } writeLock.interestedThreads++; } writeLock.lock.lock(); }