Java Code Examples for org.redisson.api.RReadWriteLock#readLock()

The following examples show how to use org.redisson.api.RReadWriteLock#readLock() . 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 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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();
}