com.liferay.portal.kernel.concurrent.ReadWriteLockRegistry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.liferay.portal.kernel Show documentation
Show all versions of com.liferay.portal.kernel Show documentation
Contains interfaces for the portal services. Interfaces are only loaded by the global class loader and are shared by all plugins.
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.kernel.concurrent;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
*
* Registry for {@link ReadWriteLock} objects with {@link ReadWriteLockKey} as
* keys. The behavior of acquiring and releasing locks is provided by a {@link
* ConcurrentHashMap}. This class is completely thread safe and ensures that
* only one {@link ReadWriteLock} exists per key.
*
*
* @author Shuyang Zhou
* @see ReadWriteLock
* @see ReadWriteLockKey
*/
public class ReadWriteLockRegistry {
public Lock acquireLock(ReadWriteLockKey> readWriteLockKey) {
ReadWriteLock readWriteLock = _readWriteLockMap.get(readWriteLockKey);
if (readWriteLock == null) {
ReadWriteLock newReadWriteLock = new ReentrantReadWriteLock();
readWriteLock = _readWriteLockMap.putIfAbsent(
readWriteLockKey, newReadWriteLock);
if (readWriteLock == null) {
readWriteLock = newReadWriteLock;
}
}
if (readWriteLockKey.isWriteLock()) {
return readWriteLock.writeLock();
}
else {
return readWriteLock.readLock();
}
}
public void releaseLock(ReadWriteLockKey> readWriteLockKey) {
if (readWriteLockKey.isWriteLock()) {
_readWriteLockMap.remove(readWriteLockKey);
}
}
private final ConcurrentMap, ReadWriteLock>
_readWriteLockMap = new ConcurrentHashMap<>();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy