本文主要是介绍Redis RCountDownLatch RSemaphore的应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、CountDownLatch允许一个或者多个线程等待其他线程完成操作。
0、设置子线程数
RCountDownLatch countDownLatch = redissonClient.getCountDownLatch(DATASOURCE_PLAN_ID + plan.getPlanId());countDownLatch.trySetCount(10);
1、等待线程
// 等待子线程完成,等待时间30scountDownLatch.await(30, TimeUnit.SECONDS);
2、子线程完成后,减一
RCountDownLatch countDownLatch = redissonClient.getCountDownLatch(redisPrefixKey + this.getPlanId());
countDownLatch.countDown();
二、Semaphore是一个计数信号量,常用于限制可以访问某些资源(物理或逻辑的)线程数目。
RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("mySemaphore");
String permitId = semaphore.acquire();
// 获取一个信号,有效期只有2秒钟。
String permitId = semaphore.acquire(2, TimeUnit.SECONDS);
// ...
semaphore.release(permitId);
这篇关于Redis RCountDownLatch RSemaphore的应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!