use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonLockHeavyTest method tryLockUnlockRLock.
@ParameterizedTest
@MethodSource("mapClasses")
public void tryLockUnlockRLock(int threads, int loops) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(threads);
for (int i = 0; i < threads; i++) {
Runnable worker = new Runnable() {
@Override
public void run() {
for (int j = 0; j < loops; j++) {
RLock lock = redisson.getLock("RLOCK_" + j);
try {
if (lock.tryLock(ThreadLocalRandom.current().nextInt(10), TimeUnit.MILLISECONDS)) {
try {
RBucket<String> bucket = redisson.getBucket("RBUCKET_" + j);
bucket.set("TEST", 30, TimeUnit.SECONDS);
RSemaphore semaphore = redisson.getSemaphore("SEMAPHORE_" + j);
semaphore.release();
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
} finally {
lock.unlock();
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
executor.execute(worker);
}
executor.shutdown();
executor.awaitTermination(threads * loops, TimeUnit.SECONDS);
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonLockTest method testForceUnlock.
@Test
public void testForceUnlock() {
RLock lock = redisson.getLock("lock");
lock.lock();
lock.forceUnlock();
Assertions.assertFalse(lock.isLocked());
lock = redisson.getLock("lock");
Assertions.assertFalse(lock.isLocked());
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonFairLockTest method testExpire.
@Test
public void testExpire() throws InterruptedException {
RLock lock = redisson.getFairLock("lock");
lock.lock(2, TimeUnit.SECONDS);
final long startTime = System.currentTimeMillis();
Thread t = new Thread() {
public void run() {
RLock lock1 = redisson.getFairLock("lock");
lock1.lock();
long spendTime = System.currentTimeMillis() - startTime;
System.out.println(spendTime);
Assertions.assertTrue(spendTime < 2020);
lock1.unlock();
}
};
t.start();
t.join();
lock.unlock();
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonFairLockTest method testUnlockFail.
@Test
public void testUnlockFail() {
Assertions.assertThrows(IllegalMonitorStateException.class, () -> {
RLock lock = redisson.getFairLock("lock");
Thread t = new Thread() {
public void run() {
RLock lock = redisson.getFairLock("lock");
lock.lock();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lock.unlock();
}
};
t.start();
t.join(400);
try {
lock.unlock();
} catch (IllegalMonitorStateException e) {
t.join();
throw e;
}
});
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonFairLockTest method testForceUnlock.
@Test
public void testForceUnlock() {
RLock lock = redisson.getFairLock("lock");
lock.lock();
lock.forceUnlock();
Assertions.assertFalse(lock.isLocked());
lock = redisson.getFairLock("lock");
Assertions.assertFalse(lock.isLocked());
}
Aggregations