十、curator recipes之信号量InterProcessSemaphoreV2
简介
跟Java并信号量没有什么不同,curator实现的信号量也是基于令牌桶算法,当一个线程要执行的时候就去桶里面获取令牌,如果有足够的令牌那么我就执行如果没有那么我就阻塞,当线程执行完毕也要将令牌放回桶里。
官方文档:http://curator.apache.org/curator-recipes/shared-semaphore.html
代码示例
以下示例中,我们设置了信号量为1,如果其中一个线程取走了,那么下一个线程将阻塞直接信号量被返回到桶里面。
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreV2;
import org.apache.curator.framework.recipes.locks.Lease;
import org.apache.curator.retry.ExponentialBackoffRetry; public class InterProcessSemaphoreDemo {
private static CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(3000, 2));
private static String path = "/semaphore/0001";
static {
client.start();
} public static void main(String[] args) throws InterruptedException {
startThread0();
startThread1();
Thread.sleep(50000);
client.close();
} private static void startThread1() {
new Thread(() -> {
InterProcessSemaphoreV2 semaphoreV2 = new InterProcessSemaphoreV2(client, path, 1);
Lease lease = null;
try {
System.out.println("thread0 acquiring");
lease = semaphoreV2.acquire();
System.out.println("thread0 acquired and sleeping");
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
} finally {
semaphoreV2.returnLease(lease);
System.out.println("thread0 return lease");
}
}).start();
} private static void startThread0() {
new Thread(() -> {
InterProcessSemaphoreV2 semaphoreV2 = new InterProcessSemaphoreV2(client, path, 1);
Lease lease = null;
try {
System.out.println("thread1 acquiring");
lease = semaphoreV2.acquire();
System.out.println("thread1 acquired and sleeping");
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
} finally {
semaphoreV2.returnLease(lease);
System.out.println("thread1 return lease");
}
}).start();
}
}
十、curator recipes之信号量InterProcessSemaphoreV2的更多相关文章
- 二十、curator recipes之NodeCache
简介 Curator的NodeCache允许你监听一个节点,当节点数据更改或者节点被删除的时候将会触发监听. 官方文档:http://curator.apache.org/curator-recipe ...
- 十九、curator recipes之PathChildrenCache
简介 curator可以监听路径下子节点的变更操作,如创建节点,删除节点 官方文档:http://curator.apache.org/curator-recipes/path-cache.html ...
- 十八、curator recipes之DistributedDelayQueue
简介 curator实现了类似DelayQueue的分布式延迟队列 官方文档:http://curator.apache.org/curator-recipes/distributed-delay-q ...
- 十六、curator recipes之DistributedIdQueue
简介 curator实现了一种分布式ID队列,也是遵循FIFO原则,比普通队列新增的一个点是ID队列可以根据ID对队列元素进行操作,比如移除该元素. 官方文档:http://curator.apach ...
- 十五、curator recipes之DistributedQueue
简介 curator实现了先入先出的分布式消息队列,它采用的是zookeeper的持久化有序节点. 官方文档:http://curator.apache.org/curator-recipes/dis ...
- 十四、curator recipes之DistributedAtomicLong
简介 和Java的AtomicLong没有太大的不同DistributedAtomicLong旨在分布式场景中维护一个Long类型的数据,你可以像普通单机环境一样来使用它. 官方文档:http://c ...
- 十二、curator recipes之双重屏障DoubleBarrier
简介 curator实现了单个屏障barrier和双重屏障DoubleBarrier,单个屏障就是在一个进程里面设置了屏障,并等待其它进程去移除这个屏障,否则一直阻塞.双重屏障就是设置了两道屏障,两个 ...
- 二十一、curator recipes之TreeCache
简介 curator的TreeCache允许对某个路径的数据和路径变更以及其下所有子孙节点的数据和路径变更进行监听. 官方文档:http://curator.apache.org/curator-re ...
- 十七、curator recipes之DistributedPriorityQueue
简介 官方文档:http://curator.apache.org/curator-recipes/distributed-priority-queue.html javaDoc:http://cur ...
随机推荐
- 【Oracle 12c】最新CUUG OCP-071考试题库(60题)
60.(16-10) choose the best answer: Evaluate the following SQL commands: SQL>CREATE SEQUENCE ord_s ...
- “全栈2019”Java多线程第五章:线程睡眠sleep()方法详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java异常第二十一章:finally不被执行的情况
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- InnoDB: The innodb_system data file 'ibdata1' must be writable错误
重新安装percona5.7过程中,启动mysql服务总是报如下的错误 --10T02::.781070Z [ERROR] InnoDB: The innodb_system data file 'i ...
- CTF常见加密方式汇总
1.栅栏密码 在IDF训练营里做过一道关于栅栏密码的问题. 栅栏密码的解法很简单,也有点复杂,字符长度因数多得会有很多个密码.对,栅栏密码的解法就是:计算该字符串是否为合数,若为合数,则求出该合数除本 ...
- 关于在JS中AJAX导致跨域问题的解决
在部署一个原声的前端项目的时候,请求该服务器后端接口时发现出现了CORS跨域的问题,但是服务端已经做了同源策略的兼容,常见问题,遂记录. 报错信息: XMLHttpRequest cannot loa ...
- java开发注解大全
目录 1.最基础注解(spring-context包下的org.springframework.stereotype) 1.1.@Controller @Service @Repository @Co ...
- centos6.5安装docker(亲测)
centos6.5下安装docker的过程办法 在看了网上N多复制粘贴的文章,又尝试无效后,我把我最终成功的办法发出来,希望能帮到拼命干环境的你. 操作环境: centos6.5(Final) 内核: ...
- 【CF1157F】Maximum Balanced Circle 求一个相邻元素之间绝对值为小于1的最大环
题目: https://codeforces.com/contest/1157/problem/F 给出一个序列 , 我们要从序列里面挑出一些数构造成一个相邻元素之间绝对值为小于1的最大环 , 挑选的 ...
- 【Guava】Optional接口来避免空指针错误
null会带来很多问题,从开始有null开始有无数程序栽在null的手里,null的含义是不清晰的,检查null在大多数情况下是不得不做的,而我们又在很多时候忘记了对null做检查,在我们的产品真正投 ...