十、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 ...
随机推荐
- 【12c OCP】CUUG OCP认证071考试原题解析(33)
33.choose the best answer View the Exhibit and examine the structure of the ORDER_ITEMS table. Exami ...
- 【OCP|052】OCP最新题库解析系列-2
2.Which two are true about Optimizer Statistics? ❑ A) They do not persist across Instance restarts. ...
- P4016 负载平衡问题 网络流
P4016 负载平衡问题 题目描述 GG 公司有 nn 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 nn个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运 ...
- “全栈2019”Java异常第十二章:catch与异常匹配
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- 洛谷P1393 动态逆序对(CDQ分治)
传送门 题解 听别人说这是洛谷用户的双倍经验啊……然而根本没有感觉到……因为另外的那题我是用树状数组套主席树做的……而且莫名其妙感觉那种方法思路更清晰(虽然码量稍稍大了那么一点点)……感谢Candy大 ...
- NOIp2018提高组游记
Day1 T1 积木大赛 NOIp2013D2T1.....看到的时候我还以为我记错了,以为原题是一次可以随便加,这题只能加一,出考场后查了下发现一模一样. #include <iostream ...
- Metasploit渗透某高校域服务器
本文作者:i 春秋签约作家——shuteer 前期准备:1. 使用Veil生成免杀PAYLOAD: 2. 有一个外网IP的服务器,安装好metasploit,方便操作. 一.SHELL反弹meterp ...
- set,env和export命令显示shell变量其区别,与环境变量扫盲(一)
种类: ♦ set : 用来显示本地变量,显示当前shell的变量,包括当前用户的变量 ♦ env : 用来显示环境变量,显示当前用户的变量. ♦ export: 用来显示和设置环 ...
- HBTS(HBOI) 2019 真实退役记
Day 0 早上迷迷糊糊醒了不知道多久,反正差不多的时间被叫醒了,然后走去了火车站. 这次终于取到了蓝色的车票,以前去武汉的车票都取的红色不知道为什么-- 在火车上看了<悲伤逆流成河>,稍 ...
- (三)Audio子系统之AudioRecord.startRecording
在上一篇文章<(二)Audio子系统之new AudioRecord()>中已经介绍了Audio系统如何创建AudioRecord对象以及输入流,并创建了RecordThread线程,接下 ...