Search in sources :

Example 11 with RReadWriteLock

use of org.redisson.api.RReadWriteLock in project redisson by redisson.

the class RedissonReadWriteLockTest method testWriteRead.

@Test
public void testWriteRead() throws InterruptedException {
    RReadWriteLock readWriteLock = redisson.getReadWriteLock("TEST");
    readWriteLock.writeLock().lock();
    int threads = 20;
    CountDownLatch ref = new CountDownLatch(threads);
    for (int i = 0; i < threads; i++) {
        Thread t1 = new Thread(() -> {
            readWriteLock.readLock().lock();
            try {
                Thread.sleep(800);
            } catch (InterruptedException e) {
            }
            readWriteLock.readLock().unlock();
            ref.countDown();
        });
        t1.start();
        t1.join(100);
    }
    readWriteLock.writeLock().unlock();
    assertThat(ref.await(1, TimeUnit.SECONDS)).isTrue();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 12 with RReadWriteLock

use of org.redisson.api.RReadWriteLock in project redisson by redisson.

the class RedissonReadWriteLockTest method testConcurrencyLoop_MultiInstance.

@Test
public void testConcurrencyLoop_MultiInstance() throws InterruptedException {
    final int iterations = 100;
    final AtomicInteger lockedCounter = new AtomicInteger();
    final Random r = new SecureRandom();
    testMultiInstanceConcurrency(16, rc -> {
        for (int i = 0; i < iterations; i++) {
            boolean useWriteLock = r.nextBoolean();
            RReadWriteLock rwlock = rc.getReadWriteLock("testConcurrency_MultiInstance1");
            RLock lock;
            if (useWriteLock) {
                lock = rwlock.writeLock();
            } else {
                lock = rwlock.readLock();
            }
            lock.lock();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lockedCounter.incrementAndGet();
            rwlock = rc.getReadWriteLock("testConcurrency_MultiInstance1");
            if (useWriteLock) {
                lock = rwlock.writeLock();
            } else {
                lock = rwlock.readLock();
            }
            lock.unlock();
        }
    });
    Assertions.assertEquals(16 * iterations, lockedCounter.get());
}
Also used : Random(java.util.Random) SecureRandom(java.security.SecureRandom) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SecureRandom(java.security.SecureRandom) RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 13 with RReadWriteLock

use of org.redisson.api.RReadWriteLock in project redisson by redisson.

the class RedissonReadWriteLockTest method testWR.

@Test
public void testWR() throws InterruptedException {
    RReadWriteLock rw = redisson.getReadWriteLock("my_read_write_lock");
    RLock writeLock = rw.writeLock();
    writeLock.lock();
    rw.readLock().lock();
    assertThat(writeLock.isLocked()).isTrue();
    rw.readLock().unlock();
    assertThat(writeLock.isLocked()).isTrue();
    writeLock.unlock();
    assertThat(writeLock.isLocked()).isFalse();
}
Also used : RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 14 with RReadWriteLock

use of org.redisson.api.RReadWriteLock in project redisson by redisson.

the class RedissonReadWriteLockTest method testConcurrency_MultiInstance.

@Test
public void testConcurrency_MultiInstance() throws InterruptedException {
    int iterations = 100;
    final AtomicInteger lockedCounter = new AtomicInteger();
    final Random r = new SecureRandom();
    testMultiInstanceConcurrency(iterations, rc -> {
        RReadWriteLock rwlock = rc.getReadWriteLock("testConcurrency_MultiInstance2");
        RLock lock;
        if (r.nextBoolean()) {
            lock = rwlock.writeLock();
        } else {
            lock = rwlock.readLock();
        }
        lock.lock();
        lockedCounter.incrementAndGet();
        lock.unlock();
    });
    Assertions.assertEquals(iterations, lockedCounter.get());
}
Also used : Random(java.util.Random) SecureRandom(java.security.SecureRandom) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SecureRandom(java.security.SecureRandom) RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 15 with RReadWriteLock

use of org.redisson.api.RReadWriteLock in project redisson by redisson.

the class RedissonReadWriteLockTest method testExpireWrite.

@Test
public void testExpireWrite() throws InterruptedException {
    RReadWriteLock lock = redisson.getReadWriteLock("lock");
    lock.writeLock().lock(2, TimeUnit.SECONDS);
    final long startTime = System.currentTimeMillis();
    Thread t = new Thread() {

        public void run() {
            RReadWriteLock lock1 = redisson.getReadWriteLock("lock");
            lock1.writeLock().lock();
            long spendTime = System.currentTimeMillis() - startTime;
            Assertions.assertTrue(spendTime < 2050);
            lock1.writeLock().unlock();
        }
    };
    t.start();
    t.join();
    lock.writeLock().unlock();
}
Also used : RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Aggregations

RReadWriteLock (org.redisson.api.RReadWriteLock)26 Test (org.junit.jupiter.api.Test)25 RLock (org.redisson.api.RLock)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 SecureRandom (java.security.SecureRandom)3 Random (java.util.Random)3 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Future (java.util.concurrent.Future)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1 ClusterProcesses (org.redisson.ClusterRunner.ClusterProcesses)1 RedissonClient (org.redisson.api.RedissonClient)1 Config (org.redisson.config.Config)1