org.redisson.api.RReadWriteLock Java Examples

The following examples show how to use org.redisson.api.RReadWriteLock. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedissonReadWriteLockTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testWriteLockExpiration() throws InterruptedException {
    RReadWriteLock rw1 = redisson.getReadWriteLock("test2s3");
    
    RLock l1 = rw1.writeLock();
    assertThat(l1.tryLock(10000, 10000, TimeUnit.MILLISECONDS)).isTrue();
    RLock l2 = rw1.writeLock();
    assertThat(l2.tryLock(1000, 1000, TimeUnit.MILLISECONDS)).isTrue();

    await().atMost(Duration.TEN_SECONDS).until(() -> {
        RReadWriteLock rw2 = redisson.getReadWriteLock("test2s3");
        try {
            return !rw2.writeLock().tryLock(3000, 1000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }
    });
}
 
Example #2
Source File: RedissonReadWriteLockTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@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();
    });

    Assert.assertEquals(iterations, lockedCounter.get());
}
 
Example #3
Source File: RedissonReadWriteLockTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testConcurrency_SingleInstance() throws InterruptedException {
    final AtomicInteger lockedCounter = new AtomicInteger();

    final Random r = new SecureRandom();
    int iterations = 15;
    testSingleInstanceConcurrency(iterations, rc -> {
        RReadWriteLock rwlock = rc.getReadWriteLock("testConcurrency_SingleInstance");
        RLock lock;
        if (r.nextBoolean()) {
            lock = rwlock.writeLock();
        } else {
            lock = rwlock.readLock();
        }
        lock.lock();
        lockedCounter.incrementAndGet();
        lock.unlock();
    });

    Assert.assertEquals(iterations, lockedCounter.get());
}
 
Example #4
Source File: RedissonReadWriteLockTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@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;
            Assert.assertTrue(spendTime < 2050);
            lock1.writeLock().unlock();
        };
    };

    t.start();
    t.join();

    lock.writeLock().unlock();
}
 
Example #5
Source File: RedissonReadWriteLockTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testExpireRead() throws InterruptedException {
    RReadWriteLock lock = redisson.getReadWriteLock("lock");
    lock.readLock().lock(2, TimeUnit.SECONDS);

    final long startTime = System.currentTimeMillis();
    Thread t = new Thread() {
        public void run() {
            RReadWriteLock lock1 = redisson.getReadWriteLock("lock");
            lock1.readLock().lock();
            long spendTime = System.currentTimeMillis() - startTime;
            Assert.assertTrue(spendTime < 2050);
            lock1.readLock().unlock();
        };
    };

    t.start();
    t.join();

    lock.readLock().unlock();
}
 
Example #6
Source File: RedissonReadWriteLockTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testWriteReadReentrancy() throws InterruptedException {
    RReadWriteLock readWriteLock = redisson.getReadWriteLock("TEST");
    readWriteLock.writeLock().lock();

    java.util.concurrent.locks.Lock rLock = readWriteLock.readLock();
    Assert.assertTrue(rLock.tryLock());
    
    AtomicBoolean ref = new AtomicBoolean();
    Thread t1 = new Thread(() -> {
        boolean success = readWriteLock.readLock().tryLock();
        ref.set(success);
    });
    t1.start();
    t1.join();
    
    Assert.assertFalse(ref.get());
    
    readWriteLock.writeLock().unlock();
    Assert.assertFalse(readWriteLock.writeLock().tryLock());
    rLock.unlock();

    Assert.assertTrue(readWriteLock.writeLock().tryLock());
    readWriteLock.writeLock().unlock();
}
 
Example #7
Source File: RedissonReadWriteLockTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #8
Source File: RedissonReadWriteLockTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testInCluster() throws Exception {
    RedisRunner master1 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner master2 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner master3 = new RedisRunner().randomPort().randomDir().nosave();

    ClusterRunner clusterRunner = new ClusterRunner()
            .addNode(master1)
            .addNode(master2)
            .addNode(master3);
    ClusterProcesses process = clusterRunner.run();
    
    Config config = new Config();
    config.useClusterServers()
    .addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    
    RReadWriteLock s = redisson.getReadWriteLock("1234");
    s.writeLock().lock();
    s.readLock().lock();
    s.readLock().unlock();
    s.writeLock().unlock();
    
    redisson.shutdown();
    process.shutdown();
}
 
Example #9
Source File: RedissonReadWriteLockTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@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();
        }
    });

    Assert.assertEquals(16 * iterations, lockedCounter.get());
}
 
Example #10
Source File: RedissonReadWriteLockTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testLockUnlock() {
    RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
    RLock lock = rwlock.readLock();
    lock.lock();
    lock.unlock();

    lock.lock();
    lock.unlock();
}
 
Example #11
Source File: RedissonReadWriteLockTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsLocked() {
    RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
    RLock lock = rwlock.readLock();
    Assert.assertFalse(lock.isLocked());
    lock.lock();
    Assert.assertTrue(lock.isLocked());
    lock.unlock();
    Assert.assertFalse(lock.isLocked());
}
 
Example #12
Source File: RedissonReadWriteLockTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsHeldByCurrentThread() {
    RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
    RLock lock = rwlock.readLock();
    Assert.assertFalse(lock.isHeldByCurrentThread());
    lock.lock();
    Assert.assertTrue(lock.isHeldByCurrentThread());
    lock.unlock();
    Assert.assertFalse(lock.isHeldByCurrentThread());
}
 
Example #13
Source File: RedissonReadWriteLockTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testName() throws InterruptedException, ExecutionException, TimeoutException {
    ExecutorService service = Executors.newFixedThreadPool(10);
    RReadWriteLock rwlock = redisson.getReadWriteLock("{test}:abc:key");
    RLock rlock = rwlock.readLock();

    List<Callable<Void>> callables = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
      callables.add(() -> {
        for (int j = 0; j < 10; j++) {
          rlock.lock();
          try {
          } finally {
            rlock.unlock();
          }
        }
        return null;
      });
    }

    List<Future<Void>> futures = service.invokeAll(callables);
    for (Future<Void> future : futures) {
        assertThatCode(future::get).doesNotThrowAnyException();
    }

    service.shutdown();
    assertThat(service.awaitTermination(1, TimeUnit.MINUTES)).isTrue();
}
 
Example #14
Source File: RedisAtom.java    From Milkomeda with MIT License 5 votes vote down vote up
private RLock getLock(String keyPath, AtomLockType type, boolean readOnly) {
    switch (type) {
        case FAIR:
            return redisson.getFairLock(keyPath);
        case READ_WRITE:
            RReadWriteLock readWriteLock = redisson.getReadWriteLock(keyPath);
            return readOnly ? readWriteLock.readLock() : readWriteLock.writeLock();
        default:
            return redisson.getLock(keyPath);
    }
}
 
Example #15
Source File: ReadWriteLockExamples.java    From redisson-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();

    final RReadWriteLock lock = redisson.getReadWriteLock("lock");

    lock.writeLock().tryLock();

    Thread t = new Thread() {
        public void run() {
             RLock r = lock.readLock();
             r.lock();

             try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            r.unlock();
        };
    };

    t.start();
    t.join();

    lock.writeLock().unlock();

    t.join();
    
    redisson.shutdown();
}
 
Example #16
Source File: RedissonReadWriteLockTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #17
Source File: TracingRMap.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RReadWriteLock getReadWriteLock(K key) {
  return new TracingRReadWriteLock(map.getReadWriteLock(key), tracingRedissonHelper);
}
 
Example #18
Source File: TracingRSetCache.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RReadWriteLock getReadWriteLock(V value) {
  return new TracingRReadWriteLock(cache.getReadWriteLock(value), tracingRedissonHelper);
}
 
Example #19
Source File: TracingRReadWriteLock.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
public TracingRReadWriteLock(RReadWriteLock lock, TracingRedissonHelper tracingRedissonHelper) {
  this.lock = lock;
  this.tracingRedissonHelper = tracingRedissonHelper;
}
 
Example #20
Source File: RedissonReadWriteLockTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test
public void testHoldCount() {
    RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
    testHoldCount(rwlock.readLock());
    testHoldCount(rwlock.writeLock());
}
 
Example #21
Source File: TracingRedissonClient.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RReadWriteLock getReadWriteLock(String name) {
  return new TracingRReadWriteLock(redissonClient.getReadWriteLock(name), tracingRedissonHelper);
}
 
Example #22
Source File: TracingRSet.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RReadWriteLock getReadWriteLock(V value) {
  return new TracingRReadWriteLock(set.getReadWriteLock(value), tracingRedissonHelper);
}
 
Example #23
Source File: RedissonReadWriteLockTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultiRead() throws InterruptedException {
    final RReadWriteLock lock = redisson.getReadWriteLock("lock");
    Assert.assertFalse(lock.readLock().forceUnlock());

    final RLock readLock1 = lock.readLock();
    readLock1.lock();

    Assert.assertFalse(lock.writeLock().tryLock());

    final AtomicReference<RLock> readLock2 = new AtomicReference<RLock>();
    Thread t = new Thread() {
        public void run() {
             RLock r = lock.readLock();
             Assert.assertFalse(readLock1.isHeldByCurrentThread());
             Assert.assertTrue(readLock1.isLocked());
             r.lock();
             readLock2.set(r);

             try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            r.unlock();
        };
    };

    t.start();
    t.join(50);

    Assert.assertTrue(readLock2.get().isLocked());

    readLock1.unlock();
    Assert.assertFalse(lock.writeLock().tryLock());
    Assert.assertFalse(readLock1.isHeldByCurrentThread());
    Thread.sleep(1000);

    Assert.assertFalse(readLock2.get().isLocked());
    Assert.assertTrue(lock.writeLock().tryLock());
    Assert.assertTrue(lock.writeLock().isLocked());
    Assert.assertTrue(lock.writeLock().isHeldByCurrentThread());
    lock.writeLock().unlock();

    Assert.assertFalse(lock.writeLock().isLocked());
    Assert.assertFalse(lock.writeLock().isHeldByCurrentThread());
    Assert.assertTrue(lock.writeLock().tryLock());
    lock.writeLock().forceUnlock();
}
 
Example #24
Source File: RedissonReadWriteLockTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test
public void testWriteLock() throws InterruptedException {
    final RReadWriteLock lock = redisson.getReadWriteLock("lock");

    final RLock writeLock = lock.writeLock();
    writeLock.lock();

    Assert.assertTrue(lock.writeLock().tryLock());

    Thread t = new Thread() {
        public void run() {
             Assert.assertFalse(writeLock.isHeldByCurrentThread());
             Assert.assertTrue(writeLock.isLocked());
             Assert.assertFalse(lock.readLock().tryLock());
             Assert.assertFalse(redisson.getReadWriteLock("lock").readLock().tryLock());

             try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

             Assert.assertTrue(lock.readLock().tryLock());
             Assert.assertTrue(redisson.getReadWriteLock("lock").readLock().tryLock());
        };
    };

    t.start();
    t.join(50);

    writeLock.unlock();
    Assert.assertTrue(lock.readLock().tryLock());
    Assert.assertTrue(writeLock.isHeldByCurrentThread());
    writeLock.unlock();
    Thread.sleep(1000);

    Assert.assertFalse(lock.writeLock().tryLock());
    Assert.assertFalse(lock.writeLock().isLocked());
    Assert.assertFalse(lock.writeLock().isHeldByCurrentThread());
    lock.writeLock().forceUnlock();
}
 
Example #25
Source File: RedissonTransactionalMapCache.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RReadWriteLock getReadWriteLock(K key) {
    throw new UnsupportedOperationException("getReadWriteLock method is not supported in transaction");
}
 
Example #26
Source File: TracingRMultimap.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RReadWriteLock getReadWriteLock(K key) {
  return new TracingRReadWriteLock(map.getReadWriteLock(key), tracingRedissonHelper);
}
 
Example #27
Source File: RedissonMap.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RReadWriteLock getReadWriteLock(K key) {
    String lockName = getLockByMapKey(key, "rw_lock");
    return new RedissonReadWriteLock(commandExecutor, lockName);
}
 
Example #28
Source File: RedissonSetMultimapValues.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RReadWriteLock getReadWriteLock(V value) {
    return set.getReadWriteLock(value);
}
 
Example #29
Source File: RedissonSet.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RReadWriteLock getReadWriteLock(V value) {
    String lockName = getLockByValue(value, "rw_lock");
    return new RedissonReadWriteLock(commandExecutor, lockName);
}
 
Example #30
Source File: RedissonSetCache.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RReadWriteLock getReadWriteLock(V value) {
    String lockName = getLockByValue(value, "rw_lock");
    return new RedissonReadWriteLock(commandExecutor, lockName);
}