六、curator recipes之屏障barrier
简介
curator针对分布式场景实现了分布式屏障:barrier。我们在分布式系统中可以使用barrier去阻塞进程,知道某个条件被触发。其实跟Java多线程的barrier是一样的。
例如:当两个进程在执行任务的时候,A调用了B,A需要等待B完成以后的通知
官方文档:http://curator.apache.org/curator-recipes/barrier.html
代码示例
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.barriers.DistributedBarrier;
import org.apache.curator.retry.ExponentialBackoffRetry; public class Barrier {
private static CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(3000, 3));
private static String path = "/barrier/001";
public static void main(String[] args) throws Exception {
client.start();
DistributedBarrier barrier = new DistributedBarrier(client, path);
barrier.setBarrier();
System.out.println("set barrier");
notifyTo();
System.out.println("wait barrier");
barrier.waitOnBarrier();
System.out.println("wait end");
client.close();
} public static void notifyTo() {
new Thread(() -> {
DistributedBarrier barrier = new DistributedBarrier(client, path);
try {
System.out.println("notify sleep...");
Thread.sleep(3000);
barrier.removeBarrier();
System.out.println("notify remove barrier");
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}
输出结果
set barrier
wait barrier
notify sleep...
notify remove barrier
wait end
主线程等待屏障被移除了以后继续执行
六、curator recipes之屏障barrier的更多相关文章
- 十二、curator recipes之双重屏障DoubleBarrier
简介 curator实现了单个屏障barrier和双重屏障DoubleBarrier,单个屏障就是在一个进程里面设置了屏障,并等待其它进程去移除这个屏障,否则一直阻塞.双重屏障就是设置了两道屏障,两个 ...
- ZooKeeper实现同步屏障(Barrier)
按照维基百科的解释:同步屏障(Barrier)是并行计算中的一种同步方法.对于一群进程或线程,程序中的一个同步屏障意味着任何线程/进程执行到此后必须等待,直到所有线程/进程都到达此点才可继续执行下文. ...
- 十六、curator recipes之DistributedIdQueue
简介 curator实现了一种分布式ID队列,也是遵循FIFO原则,比普通队列新增的一个点是ID队列可以根据ID对队列元素进行操作,比如移除该元素. 官方文档:http://curator.apach ...
- 二十一、curator recipes之TreeCache
简介 curator的TreeCache允许对某个路径的数据和路径变更以及其下所有子孙节点的数据和路径变更进行监听. 官方文档:http://curator.apache.org/curator-re ...
- 二十、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之DistributedPriorityQueue
简介 官方文档:http://curator.apache.org/curator-recipes/distributed-priority-queue.html javaDoc:http://cur ...
- 十五、curator recipes之DistributedQueue
简介 curator实现了先入先出的分布式消息队列,它采用的是zookeeper的持久化有序节点. 官方文档:http://curator.apache.org/curator-recipes/dis ...
随机推荐
- Delphi XE7实现的任意位置弹出菜单
Delphi XE7中目前还没有弹出菜单组件,这个弹出菜单应用很普遍,在JAVA开发的安卓程序中很简单就可以用上了,应该说是一个标准控件.看了一些例子,但是都不能满足我想在任意位置弹出菜单需求,于是自 ...
- 2018-2019-2 20165219《网络对抗技术》Exp4 恶意代码分析
基础问题回答 实验目的 监控系统的运行状态,看有没有可疑的程序在运行 分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sysinternals,systracer套 ...
- 【SSH学习笔记】用Struts2实现简单的用户登录
准备阶段 在使用学习Struts2的时候首先要下载相应的架包 Struts2资源下载 这里建议下载第一个,在struts-2.5.14.1-all.zip里有很多实用的东西,不仅有架包还有官方为开发者 ...
- [Objective-C语言教程]日志处理(21)
为了打印日志,可使用Objective-C编程语言中的NSLog方法,首先在HelloWorld示例中使用了这个方法. 下面来看一下打印“Hello World”字样的简单代码 - #import & ...
- Mysql6.0连接中的几个问题 Mysql6.xx
Mysql6.0连接中的几个问题 在最近做一些Javaweb整合时,因为我在maven官网查找的资源,使用的最新版,6.0.3,发现MySQL连接中的几个问题,总结如下: 1.Loading clas ...
- Linux磁盘分区、挂在
分区基础知识分区的方式:1) mbr分区:1.最多支持四个主分区2.系统只能安装在主分区3.扩展分区要占一个主分区4.MBR最大只支持2TB,但拥有最好的兼容性2) gtp分区:1.支持无限多个主分区 ...
- c常用函数
一.strtol long int strtol(const char *nptr, char **endptr, int base) strtol()会将nptr指向的字符串,根据参数base,按权 ...
- Java switch函数
switch()函数中能放置的值为:byte,short,char,int,string,enum类型或者byte,short,char,int的包装类,其中,string类型是java7(含)之后才 ...
- #Go# 常用类型转换
#string 2 int int, err := strconv.Atoi(string) #string 2 int64 int64, err := strconv.ParseInt(string ...
- 【App性能】:TraceView分析法
抓取traceview的日志有两种方式, 1,是在代码中片段中添加: Debug.startMethodTracing(“hello”); ....... Debug.stopMethodTracin ...