Search in sources :

Example 6 with RSemaphore

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();
    }
}
Also used : RSemaphore(org.redisson.api.RSemaphore)

Example 7 with RSemaphore

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);
}
Also used : RSemaphore(org.redisson.api.RSemaphore) RLock(org.redisson.api.RLock) Test(org.junit.Test)

Example 8 with RSemaphore

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);
}
Also used : RSemaphore(org.redisson.api.RSemaphore) RLock(org.redisson.api.RLock) Test(org.junit.Test)

Example 9 with RSemaphore

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();
        }
    }
}
Also used : RSemaphore(org.redisson.api.RSemaphore)

Example 10 with RSemaphore

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);
}
Also used : RSemaphore(org.redisson.api.RSemaphore) Test(org.junit.Test)

Aggregations

RSemaphore (org.redisson.api.RSemaphore)17 Test (org.junit.Test)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 RLock (org.redisson.api.RLock)2 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1