Search in sources :

Example 11 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project ignite by apache.

the class ReadWriteLockMultiThreadedTest method testReadLockAcquire.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings({ "LockAcquiredButNotSafelyReleased" })
public void testReadLockAcquire() throws Exception {
    final ReadWriteLock lock = new ReentrantReadWriteLock();
    lock.writeLock().lock();
    X.println("Write lock acquired: " + lock);
    IgniteInternalFuture fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            X.println("Attempting to acquire read lock: " + lock);
            lock.readLock().lock();
            try {
                X.println("Read lock acquired: " + lock);
                return null;
            } finally {
                lock.readLock().unlock();
            }
        }
    }, 1, "read-lock");
    Thread.sleep(2000);
    X.println(">>> Dump threads now! <<<");
    Thread.sleep(15 * 1000);
    X.println("Write lock released.");
    lock.writeLock().unlock();
    fut.get();
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project ignite by apache.

the class ReadWriteLockMultiThreadedTest method testTryWriteLock.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings({ "LockAcquiredButNotSafelyReleased" })
public void testTryWriteLock() throws Exception {
    final ReadWriteLock lock = new ReentrantReadWriteLock();
    lock.readLock().lock();
    X.println("Read lock acquired.");
    IgniteInternalFuture fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            boolean res = lock.writeLock().tryLock();
            X.println("Attempting to try write lock: " + res);
            try {
                return null;
            } finally {
                if (res)
                    lock.writeLock().unlock();
            }
        }
    }, 1, "write-lock");
    Thread.sleep(2000);
    X.println("Read lock released: " + lock);
    lock.readLock().unlock();
    fut.get();
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project jackrabbit-oak by apache.

the class TarRevisions method setHead.

/**
     * This implementation blocks if a concurrent call to
     * {@link #setHead(Function, Option...)} is already in
     * progress.
     *
     * @param options   zero or one expedite option for expediting this call
     * @throws IllegalArgumentException  on any non recognised {@code option}.
     * @see #EXPEDITE_OPTION
     */
@Override
public boolean setHead(@Nonnull RecordId expected, @Nonnull RecordId head, @Nonnull Option... options) {
    checkBound();
    // If the expedite option was specified we acquire the write lock instead of the read lock.
    // This will cause this thread to get the lock before all threads currently waiting to
    // enter the read lock. See also the class comment of ReadWriteLock.
    Lock lock = isExpedited(options) ? rwLock.writeLock() : rwLock.readLock();
    lock.lock();
    try {
        RecordId id = this.head.get();
        return id.equals(expected) && this.head.compareAndSet(id, head);
    } finally {
        lock.unlock();
    }
}
Also used : RecordId(org.apache.jackrabbit.oak.segment.RecordId) FileStoreUtil.findPersistedRecordId(org.apache.jackrabbit.oak.segment.file.FileStoreUtil.findPersistedRecordId) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Example 14 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project jgnash by ccavanaugh.

the class DistributedLockTest method writeLockTest.

@Test
public void writeLockTest() throws InterruptedException {
    class ReadLockThread implements Runnable {

        private final ReadWriteLock readWriteLock;

        private ReadLockThread(final ReadWriteLock readWriteLock) {
            this.readWriteLock = readWriteLock;
        }

        @Override
        public void run() {
            try {
                logger.info("try read lock");
                readWriteLock.readLock().lock();
                logger.info("got read lock");
            } finally {
                logger.info("unlock read lock");
                readWriteLock.readLock().unlock();
            }
        }
    }
    class WriteLockThread implements Runnable {

        private final ReadWriteLock readWriteLock;

        private WriteLockThread(final ReadWriteLock readWriteLock) {
            this.readWriteLock = readWriteLock;
        }

        @Override
        public void run() {
            try {
                logger.info("try write lock");
                readWriteLock.writeLock().lock();
                logger.info("got write lock");
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
            } finally {
                logger.info("unlock write lock");
                readWriteLock.writeLock().unlock();
            }
        }
    }
    ReadWriteLock rwLock = manager.getLock("test-lock");
    Thread writeLockThread = new Thread(new WriteLockThread(rwLock));
    Thread readLockThread = new Thread(new ReadLockThread(rwLock));
    writeLockThread.start();
    Thread.sleep(500);
    readLockThread.start();
    writeLockThread.join();
    readLockThread.join();
}
Also used : ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Test(org.junit.Test)

Example 15 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project jgnash by ccavanaugh.

the class DistributedLockTest method multipleReadLocks.

@Test
public void multipleReadLocks() {
    class WriteLockTest extends Thread {

        @Override
        public void run() {
            ReadWriteLock lock = manager.getLock("lock");
            logger.log(Level.INFO, "locking: {0}", getId());
            lock.readLock().lock();
            try {
                Random random = new Random();
                int num = random.nextInt(3000);
                try {
                    Thread.sleep(num);
                } catch (final InterruptedException e) {
                    logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
                    fail();
                }
            } finally {
                logger.log(Level.INFO, "unlocking: {0}", getId());
                lock.readLock().unlock();
            }
        }
    }
    Thread thread1 = new WriteLockTest();
    Thread thread2 = new WriteLockTest();
    Thread thread3 = new WriteLockTest();
    Thread thread4 = new WriteLockTest();
    thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
    try {
        thread1.join();
        thread2.join();
        thread3.join();
        thread4.join();
    } catch (final InterruptedException e) {
        Logger.getLogger(DistributedLockTest.class.getName()).log(Level.SEVERE, e.getLocalizedMessage(), e);
        fail();
    }
    assertTrue(true);
}
Also used : Random(java.util.Random) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Test(org.junit.Test)

Aggregations

ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)45 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)36 Lock (java.util.concurrent.locks.Lock)21 Test (org.junit.Test)8 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)4 Nullable (org.jetbrains.annotations.Nullable)4 ArrayList (java.util.ArrayList)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ExecutorService (java.util.concurrent.ExecutorService)2 Ignore (org.junit.Ignore)2 Member (cz.metacentrum.perun.core.api.Member)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Random (java.util.Random)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 Phaser (java.util.concurrent.Phaser)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1