use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonMultiLockTest method testMultiThreads.
@Test
public void testMultiThreads() throws IOException, InterruptedException {
RedisProcess redis1 = redisTestMultilockInstance();
Config config1 = new Config();
config1.useSingleServer().setAddress(redis1.getRedisServerAddressAndPort());
RedissonClient client = Redisson.create(config1);
RLock lock1 = client.getLock("lock1");
RLock lock2 = client.getLock("lock2");
RLock lock3 = client.getLock("lock3");
Thread t = new Thread() {
public void run() {
RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
lock.lock();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
lock.unlock();
}
};
t.start();
t.join(1000);
RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
lock.lock();
lock.unlock();
client.shutdown();
assertThat(redis1.stop()).isEqualTo(0);
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testIsLockedOtherThread.
@Test
public void testIsLockedOtherThread() throws InterruptedException {
RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
RLock lock = rwlock.readLock();
lock.lock();
Thread t = new Thread() {
public void run() {
RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
RLock lock = rwlock.readLock();
Assertions.assertTrue(lock.isLocked());
}
};
t.start();
t.join();
lock.unlock();
Thread t2 = new Thread() {
public void run() {
RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
RLock lock = rwlock.readLock();
Assertions.assertFalse(lock.isLocked());
}
};
t2.start();
t2.join();
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testName.
@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();
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testLockUnlock.
@Test
public void testLockUnlock() {
RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
RLock lock = rwlock.readLock();
lock.lock();
lock.unlock();
lock.lock();
lock.unlock();
}
use of org.redisson.api.RLock 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());
}
Aggregations