use of java.util.concurrent.locks.ReentrantReadWriteLock in project perun by CESNET.
the class PerunLocksUtils method lockGroupMembership.
/**
* Create transaction locks for group and bind them to the transaction (as resource by Object uniqueKey)
*
* @param group the group
* @throws InternalErrorException
* @throws InterruptedException
*/
public static void lockGroupMembership(Group group) throws InternalErrorException, InterruptedException {
if (group == null)
throw new InternalErrorException("Group can't be null when creating lock for group.");
List<Lock> returnedLocks = new ArrayList<>();
try {
//TODO - On java8 use computeIfAbsent instead
//Need to investigate if we have all needed locks already in the structure or we need to create them
ReadWriteLock groupReadWriteLock = groupsLocks.get(group);
if (groupReadWriteLock == null) {
groupsLocks.putIfAbsent(group, new ReentrantReadWriteLock(true));
groupReadWriteLock = groupsLocks.get(group);
}
groupReadWriteLock.writeLock().lock();
returnedLocks.add(groupReadWriteLock.writeLock());
//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;
}
}
use of java.util.concurrent.locks.ReentrantReadWriteLock in project java8-tutorial by winterbe.
the class Lock3 method main.
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(2);
Map<String, String> map = new HashMap<>();
ReadWriteLock lock = new ReentrantReadWriteLock();
executor.submit(() -> {
lock.writeLock().lock();
try {
ConcurrentUtils.sleep(1);
map.put("foo", "bar");
} finally {
lock.writeLock().unlock();
}
});
Runnable readTask = () -> {
lock.readLock().lock();
try {
System.out.println(map.get("foo"));
ConcurrentUtils.sleep(1);
} finally {
lock.readLock().unlock();
}
};
executor.submit(readTask);
executor.submit(readTask);
ConcurrentUtils.stop(executor);
}
use of java.util.concurrent.locks.ReentrantReadWriteLock in project alluxio by Alluxio.
the class LockResourceTest method reentrantReadWriteLock.
/**
* Tests {@link LockResource} with {@link ReentrantReadWriteLock}.
*/
@Test
public void reentrantReadWriteLock() {
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
try (LockResource r1 = new LockResource(lock.readLock())) {
try (LockResource r2 = new LockResource(lock.readLock())) {
Assert.assertEquals(lock.getReadHoldCount(), 2);
Assert.assertTrue(lock.readLock().tryLock());
lock.readLock().unlock();
}
}
Assert.assertEquals(lock.getReadHoldCount(), 0);
try (LockResource r1 = new LockResource(lock.writeLock())) {
try (LockResource r2 = new LockResource(lock.readLock())) {
Assert.assertTrue(lock.isWriteLockedByCurrentThread());
Assert.assertEquals(lock.getReadHoldCount(), 1);
}
}
Assert.assertFalse(lock.isWriteLockedByCurrentThread());
Assert.assertEquals(lock.getReadHoldCount(), 0);
try (LockResource r = new LockResource(lock.readLock())) {
Assert.assertFalse(lock.writeLock().tryLock());
}
}
use of java.util.concurrent.locks.ReentrantReadWriteLock in project CorfuDB by CorfuDB.
the class MultiReadWriteLock method acquireReadLock.
/**
* Acquire a read lock. The recommended use of this method is in try-with-resources statement.
* @param address id of the lock to acquire.
* @return A closable that will free the allocations for this lock if necessary
*/
public AutoCloseableLock acquireReadLock(final Long address) {
registerLockReference(address, false);
ReentrantReadWriteLock lock = constructLockFor(address);
final ReentrantReadWriteLock.ReadLock readLock = lock.readLock();
readLock.lock();
AtomicBoolean closed = new AtomicBoolean(false);
return () -> {
if (!closed.getAndSet(true)) {
try {
readLock.unlock();
clearEventuallyLockFor(address);
} finally {
deregisterLockReference(address, false);
}
}
};
}
use of java.util.concurrent.locks.ReentrantReadWriteLock in project deltaspike by apache.
the class LockSupplierStorage method newLock.
@Override
public ReadWriteLock newLock(final AnnotatedMethod<?> method, final boolean fair) {
final String name = method.getJavaMember().getDeclaringClass().getName();
ReadWriteLock lock = locks.get(name);
if (lock == null) {
lock = new ReentrantReadWriteLock(fair);
final ReadWriteLock existing = locks.putIfAbsent(name, lock);
if (existing != null) {
lock = existing;
}
}
return lock;
}
Aggregations