Search in sources :

Example 21 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project languagetool by languagetool-org.

the class AbstractLanguageConcurrencyTest method testSpellCheckerFailure.

@Ignore("too slow to run every time")
@Test
public void testSpellCheckerFailure() throws Exception {
    String sampleText = createSampleText();
    Language language = createLanguage();
    int threadCount = Runtime.getRuntime().availableProcessors() * 10;
    int testRuns = 100;
    ReadWriteLock testWaitLock = new ReentrantReadWriteLock();
    Lock testWriteLock = testWaitLock.writeLock();
    testWriteLock.lock();
    failedTests = 0;
    List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < threadCount; i++) {
        Thread t = new Thread(new TestRunner(testWaitLock, language, testRuns, sampleText));
        t.start();
        threads.add(t);
    }
    // Release the lock and allow all TestRunner threads to do their work.
    testWriteLock.unlock();
    for (Thread t : threads) {
        t.join();
    }
    Assert.assertEquals(0, failedTests);
}
Also used : Language(org.languagetool.Language) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ArrayList(java.util.ArrayList) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 22 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project orientdb by orientechnologies.

the class OPartitionedLockManager method releaseSharedLock.

public void releaseSharedLock(final long value) {
    final int hashCode = longHashCode(value);
    final int index = index(hashCode);
    if (useSpinLock) {
        final OReadersWriterSpinLock spinLock = spinLocks[index];
        spinLock.releaseReadLock();
        return;
    }
    final ReadWriteLock rwLock = locks[index];
    final Lock lock = rwLock.readLock();
    lock.unlock();
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 23 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project orientdb by orientechnologies.

the class OPartitionedLockManager method tryAcquireSharedLock.

public boolean tryAcquireSharedLock(T value, long timeout) throws InterruptedException {
    if (useSpinLock)
        throw new IllegalStateException("Spin lock does not support try lock mode");
    final int index;
    if (value == null)
        index = 0;
    else
        index = index(value.hashCode());
    final ReadWriteLock rwLock = locks[index];
    final Lock lock = rwLock.readLock();
    return lock.tryLock(timeout, TimeUnit.MILLISECONDS);
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 24 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project orientdb by orientechnologies.

the class OPartitionedLockManager method releaseExclusiveLock.

public void releaseExclusiveLock(final int value) {
    final int index = index(value);
    if (useSpinLock) {
        OReadersWriterSpinLock spinLock = spinLocks[index];
        spinLock.releaseWriteLock();
        return;
    }
    final ReadWriteLock rwLock = locks[index];
    rwLock.writeLock().unlock();
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 25 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project orientdb by orientechnologies.

the class OPartitionedLockManager method acquireSharedLock.

@Override
public Lock acquireSharedLock(final T value) {
    final int index;
    if (value == null)
        index = 0;
    else
        index = index(value.hashCode());
    if (useSpinLock) {
        OReadersWriterSpinLock spinLock = spinLocks[index];
        spinLock.acquireReadLock();
        return new SpinLockWrapper(true, spinLock);
    }
    final ReadWriteLock rwLock = locks[index];
    final Lock lock = rwLock.readLock();
    lock.lock();
    return lock;
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

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