Search in sources :

Example 36 with ReentrantReadWriteLock

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;
    }
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 37 with ReentrantReadWriteLock

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);
}
Also used : HashMap(java.util.HashMap) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ExecutorService(java.util.concurrent.ExecutorService) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock)

Example 38 with ReentrantReadWriteLock

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());
    }
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Test(org.junit.Test)

Example 39 with ReentrantReadWriteLock

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);
            }
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock)

Example 40 with ReentrantReadWriteLock

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;
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock)

Aggregations

ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)52 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)17 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)6 Lock (java.util.concurrent.locks.Lock)5 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)5 HashMap (java.util.HashMap)4 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)4 Nullable (org.jetbrains.annotations.Nullable)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 IOException (java.io.IOException)2 List (java.util.List)2 TreeSet (java.util.TreeSet)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 PostLoad (javax.persistence.PostLoad)2 Configuration (org.apache.hadoop.conf.Configuration)2 Path (org.apache.hadoop.fs.Path)2