use of org.redisson.api.RSemaphore in project redisson by redisson.
the class JCache method sendSync.
private void sendSync(boolean sync, List<Object> msg) {
if (sync) {
RSemaphore semaphore = redisson.getSemaphore(getSyncName(msg.get(2)));
semaphore.release();
}
}
use of org.redisson.api.RSemaphore in project redisson by redisson.
the class RedissonLockHeavyTest method lockUnlockRLock.
@Test
public void lockUnlockRLock() throws Exception {
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);
lock.lock();
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();
}
}
}
};
executor.execute(worker);
}
executor.shutdown();
executor.awaitTermination(threads * loops, TimeUnit.SECONDS);
}
use of org.redisson.api.RSemaphore in project redisson by redisson.
the class RedissonLockHeavyTest method tryLockUnlockRLock.
@Test
public void tryLockUnlockRLock() throws Exception {
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.RSemaphore in project redisson by redisson.
the class JCache method waitSync.
void waitSync(List<Object> result) {
if (result.size() < 2) {
return;
}
Long syncs = (Long) result.get(result.size() - 2);
Double syncId = (Double) result.get(result.size() - 1);
if (syncs != null && syncs > 0) {
RSemaphore semaphore = redisson.getSemaphore(getSyncName(syncId));
try {
semaphore.acquire(syncs.intValue());
semaphore.delete();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
use of org.redisson.api.RSemaphore in project redisson by redisson.
the class RedissonSemaphoreTest method testBlockingNAcquire.
@Test
public void testBlockingNAcquire() throws InterruptedException {
RSemaphore s = redisson.getSemaphore("test");
s.trySetPermits(5);
s.acquire(3);
Thread t = new Thread() {
@Override
public void run() {
RSemaphore s = redisson.getSemaphore("test");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
s.release();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
s.release();
}
};
assertThat(s.availablePermits()).isEqualTo(2);
t.start();
s.acquire(4);
assertThat(s.availablePermits()).isEqualTo(0);
}
Aggregations