Java源码示例:org.redisson.api.RCountDownLatch
示例1
/**
* ��Ϣ���еĶ�����
* @throws InterruptedException
*/
@Test
public void testGetRTopicSub() throws InterruptedException {
RTopic<String> rTopic=RedissionUtils.getInstance().getRTopic(redisson, "testTopic");
// rTopic.addListener(new MessageListener<String>() {
//
// @Override
// public void onMessage(String arg0, String arg1) {
// System.out.println("�㷢������:"+arg0);
// }
// });
//�ȴ������߷�����Ϣ
RCountDownLatch rCountDownLatch=RedissionUtils.getInstance().getRCountDownLatch(redisson, "testCountDownLatch");
rCountDownLatch.trySetCount(1);
rCountDownLatch.await();
}
示例2
/**
* RCountDownLatch ӳ��Ϊredis server��string ����
* string����ֵ
* ����--�ȴ������߳��еIJ��������� �ڽ��в���
* �鿴���м�---->keys *
* �鿴key������--->type testCountDownLatch
* �鿴key��ֵ ---->get testCountDownLatch
*/
@Test
public void testGetRCountDownLatch() throws InterruptedException {
RCountDownLatch rCountDownLatch=RedissionUtils.getInstance().getRCountDownLatch(redisson, "testCountDownLatch");
System.out.println(rCountDownLatch.getCount());
//rCountDownLatch.trySetCount(1l);
System.out.println(rCountDownLatch.getCount());
rCountDownLatch.await(10, TimeUnit.SECONDS);
System.out.println(rCountDownLatch.getCount());
}
示例3
/**
* ��Ϣ���еķ�����
*/
@Test
public void testGetRTopicPub() {
RTopic<String> rTopic=RedissionUtils.getInstance().getRTopic(redisson, "testTopic");
System.out.println(rTopic.publish("�����Ƕ�ͯ�ڣ���Ҷ�ͯ�ڿ���"));
//��������Ϣ�� �ö����߲��ٵȴ�
RCountDownLatch rCountDownLatch=RedissionUtils.getInstance().getRCountDownLatch(redisson, "testCountDownLatch");
rCountDownLatch.countDown();
}
示例4
public static void main(String[] args) throws InterruptedException {
// connects to 127.0.0.1:6379 by default
RedissonClient redisson = Redisson.create();
ExecutorService executor = Executors.newFixedThreadPool(2);
final RCountDownLatch latch = redisson.getCountDownLatch("latch1");
latch.trySetCount(1);
executor.execute(new Runnable() {
@Override
public void run() {
latch.countDown();
}
});
executor.execute(new Runnable() {
@Override
public void run() {
try {
latch.await(550, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
}
示例5
@Test
public void testSingleCountDownAwait_SingleInstance() throws InterruptedException {
final int iterations = Runtime.getRuntime().availableProcessors()*3;
RedissonClient redisson = BaseTest.createInstance();
final RCountDownLatch latch = redisson.getCountDownLatch("latch");
latch.trySetCount(iterations);
final AtomicInteger counter = new AtomicInteger();
ExecutorService executor = Executors.newScheduledThreadPool(iterations);
for (int i = 0; i < iterations; i++) {
executor.execute(() -> {
try {
latch.await();
Assert.assertEquals(0, latch.getCount());
Assert.assertEquals(iterations, counter.get());
} catch (InterruptedException e) {
Assert.fail();
}
});
}
ExecutorService countDownExecutor = Executors.newFixedThreadPool(iterations);
for (int i = 0; i < iterations; i++) {
countDownExecutor.execute(() -> {
latch.countDown();
counter.incrementAndGet();
});
}
executor.shutdown();
Assert.assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS));
redisson.shutdown();
}
示例6
@Override
public RCountDownLatch getCountDownLatch(V value) {
return new TracingRCountDownLatch(cache.getCountDownLatch(value), tracingRedissonHelper);
}
示例7
@Override
public RCountDownLatch getCountDownLatch(String name) {
return new TracingRCountDownLatch(redissonClient.getCountDownLatch(name),
tracingRedissonHelper);
}
示例8
public TracingRCountDownLatch(RCountDownLatch latch,
TracingRedissonHelper tracingRedissonHelper) {
super(latch, tracingRedissonHelper);
this.latch = latch;
this.tracingRedissonHelper = tracingRedissonHelper;
}
示例9
@Override
public RCountDownLatch getCountDownLatch(V value) {
return new TracingRCountDownLatch(set.getCountDownLatch(value), tracingRedissonHelper);
}
示例10
@Override
public RCountDownLatch getCountDownLatch(K key) {
return new TracingRCountDownLatch(map.getCountDownLatch(key), tracingRedissonHelper);
}
示例11
@Override
public RCountDownLatch getCountDownLatch(K key) {
return new TracingRCountDownLatch(map.getCountDownLatch(key), tracingRedissonHelper);
}
示例12
@Override
public RCountDownLatch getCountDownLatch(K key) {
throw new UnsupportedOperationException("getCountDownLatch method is not supported in transaction");
}
示例13
@Override
public RCountDownLatch getCountDownLatch(V value) {
throw new UnsupportedOperationException("getCountDownLatch method is not supported in transaction");
}
示例14
@Override
public RCountDownLatch getCountDownLatch(K key) {
throw new UnsupportedOperationException("getCountDownLatch method is not supported in transaction");
}
示例15
@Override
public RCountDownLatch getCountDownLatch(V value) {
String lockName = getLockByValue(value, "countdownlatch");
return new RedissonCountDownLatch(commandExecutor, lockName);
}
示例16
@Override
public RCountDownLatch getCountDownLatch(V value) {
String lockName = getLockByValue(value, "countdownlatch");
return new RedissonCountDownLatch(commandExecutor, lockName);
}
示例17
@Override
public RCountDownLatch getCountDownLatch(V value) {
return set.getCountDownLatch(value);
}
示例18
@Override
public RCountDownLatch getCountDownLatch(K key) {
String lockName = getLockByMapKey(key, "countdownlatch");
return new RedissonCountDownLatch(commandExecutor, lockName);
}
示例19
@Test
public void testCountDown() throws InterruptedException {
RCountDownLatch latch = redisson.getCountDownLatch("latch");
latch.trySetCount(2);
Assert.assertEquals(2, latch.getCount());
latch.countDown();
Assert.assertEquals(1, latch.getCount());
latch.countDown();
Assert.assertEquals(0, latch.getCount());
latch.await();
latch.countDown();
Assert.assertEquals(0, latch.getCount());
latch.await();
latch.countDown();
Assert.assertEquals(0, latch.getCount());
latch.await();
RCountDownLatch latch1 = redisson.getCountDownLatch("latch1");
latch1.trySetCount(1);
latch1.countDown();
Assert.assertEquals(0, latch.getCount());
latch1.countDown();
Assert.assertEquals(0, latch.getCount());
latch1.await();
RCountDownLatch latch2 = redisson.getCountDownLatch("latch2");
latch2.trySetCount(1);
latch2.countDown();
latch2.await();
latch2.await();
RCountDownLatch latch3 = redisson.getCountDownLatch("latch3");
Assert.assertEquals(0, latch.getCount());
latch3.await();
RCountDownLatch latch4 = redisson.getCountDownLatch("latch4");
Assert.assertEquals(0, latch.getCount());
latch4.countDown();
Assert.assertEquals(0, latch.getCount());
latch4.await();
}
示例20
@Test
public void testDelete() throws Exception {
RCountDownLatch latch = redisson.getCountDownLatch("latch");
latch.trySetCount(1);
Assert.assertTrue(latch.delete());
}
示例21
@Test
public void testDeleteFailed() throws Exception {
RCountDownLatch latch = redisson.getCountDownLatch("latch");
Assert.assertFalse(latch.delete());
}
示例22
@Test
public void testTrySetCount() throws Exception {
RCountDownLatch latch = redisson.getCountDownLatch("latch");
assertThat(latch.trySetCount(1)).isTrue();
assertThat(latch.trySetCount(2)).isFalse();
}
示例23
@Test
public void testCount() {
RCountDownLatch latch = redisson.getCountDownLatch("latch");
assertThat(latch.getCount()).isEqualTo(0);
}
示例24
/**
* ��ȡ������
* @param redisson
* @param objectName
* @return
*/
public RCountDownLatch getRCountDownLatch(RedissonClient redisson,String objectName){
RCountDownLatch rCountDownLatch=redisson.getCountDownLatch(objectName);
return rCountDownLatch;
}