use of java.util.concurrent.locks.ReentrantReadWriteLock in project hadoop by apache.
the class AbstractNodeLabelsProvider method serviceInit.
@Override
protected void serviceInit(Configuration conf) throws Exception {
this.intervalTime = conf.getLong(YarnConfiguration.NM_NODE_LABELS_PROVIDER_FETCH_INTERVAL_MS, YarnConfiguration.DEFAULT_NM_NODE_LABELS_PROVIDER_FETCH_INTERVAL_MS);
ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
readLock = readWriteLock.readLock();
writeLock = readWriteLock.writeLock();
super.serviceInit(conf);
}
use of java.util.concurrent.locks.ReentrantReadWriteLock in project atlas by alibaba.
the class BundleLock method WriteUnLock.
public static void WriteUnLock(String bundle) {
ReentrantReadWriteLock lock = null;
synchronized (bundleIdentifierMap) {
lock = bundleIdentifierMap.get(bundle);
if (lock == null) {
return;
}
}
lock.writeLock().unlock();
}
use of java.util.concurrent.locks.ReentrantReadWriteLock in project atlas by alibaba.
the class BundleLock method ReadLock.
public static boolean ReadLock(String bundle) {
ReentrantReadWriteLock lock = null;
synchronized (bundleIdentifierMap) {
lock = bundleIdentifierMap.get(bundle);
if (lock == null) {
lock = new ReentrantReadWriteLock();
bundleIdentifierMap.put(bundle, lock);
}
}
try {
return lock.readLock().tryLock(3, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// e.printStackTrace();
return false;
}
}
use of java.util.concurrent.locks.ReentrantReadWriteLock in project atlas by alibaba.
the class BundleLock method WriteLock.
/*
* Synchronized lock.
*/
public static void WriteLock(String bundle) {
ReentrantReadWriteLock lock = null;
synchronized (bundleIdentifierMap) {
lock = bundleIdentifierMap.get(bundle);
if (lock == null) {
lock = new ReentrantReadWriteLock();
bundleIdentifierMap.put(bundle, lock);
}
}
lock.writeLock().lock();
}
use of java.util.concurrent.locks.ReentrantReadWriteLock in project perun by CESNET.
the class PerunLocksUtils method lockGroupMembership.
/**
* Create transaction locks for combination of group and member (from list of members)
* and also bind them to the transaction (as resource by Object uniqueKey)
*
* @param group the group
* @param members list of members
* @throws InternalErrorException
* @throws InterruptedException
*/
public static void lockGroupMembership(Group group, List<Member> members) throws InternalErrorException, InterruptedException {
if (group == null)
throw new InternalErrorException("Group can't be null when creating lock for group and list of members.");
if (members == null)
throw new InternalErrorException("Members can't be null or empty when creating lock for group and list of members.");
//Sort list of members strictly by ids (there is compareTo method in perunBean using ids for comparing)
Collections.sort(members);
List<Lock> returnedLocks = new ArrayList<>();
try {
//TODO - On java8 use computeIfAbsent instead
//try to lock all needed locks there
ReadWriteLock groupReadWriteLock = groupsLocks.get(group);
if (groupReadWriteLock == null) {
groupsLocks.putIfAbsent(group, new ReentrantReadWriteLock(true));
groupReadWriteLock = groupsLocks.get(group);
}
groupReadWriteLock.readLock().lock();
returnedLocks.add(groupReadWriteLock.readLock());
for (Member member : members) {
//Get members lock map by group if exists or create a new one
ConcurrentHashMap<Member, Lock> membersLocks = groupsMembersLocks.get(group);
if (membersLocks == null) {
groupsMembersLocks.putIfAbsent(group, new ConcurrentHashMap<Member, Lock>());
membersLocks = groupsMembersLocks.get(group);
}
//TODO - On java8 use computeIfAbsent instead
//Get concrete member lock from members lock map or create a new one if not exists
Lock memberLock = membersLocks.get(member);
if (memberLock == null) {
membersLocks.putIfAbsent(member, new ReentrantLock(true));
memberLock = membersLocks.get(member);
}
//Lock the lock and return it
memberLock.lock();
returnedLocks.add(memberLock);
}
//bind these locks like transaction resource
if (TransactionSynchronizationManager.getResource(uniqueKey) == null) {
TransactionSynchronizationManager.bindResource(uniqueKey, returnedLocks);
} else {
((List<Lock>) TransactionSynchronizationManager.getResource(uniqueKey)).addAll(returnedLocks);
}
} catch (Exception ex) {
//if some exception has been thrown, unlock all already locked locks
unlockAll(returnedLocks);
throw ex;
}
}
Aggregations