06.Curator Barrier

1.栅栏Barrier
/*** @param client client* @param barrierPath path to use as the barrier*/public DistributedBarrier(CuratorFramework client, String barrierPath)
- setBarrier() - 设置栅栏
- waitOnBarrier() - 等待栅栏移除
- removeBarrier() - 移除栅栏
public class DistributedBarrierExample{private static final int QTY = 5;private static final String PATH = "/examples/barrier";public static void main(String[] args) throws Exception{CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));client.start();ExecutorService service = Executors.newFixedThreadPool(QTY);DistributedBarrier controlBarrier = new DistributedBarrier(client, PATH);controlBarrier.setBarrier();for (int i = 0; i < QTY; ++i){final DistributedBarrier barrier = new DistributedBarrier(client, PATH);final int index = i;Callable<Void> task = new Callable<Void>(){@Overridepublic Void call() throws Exception{Thread.sleep((long) (3 * Math.random()));System.out.println("Client #" + index + " 等待");barrier.waitOnBarrier();System.out.println("Client #" + index + " 开始");return null;}};service.submit(task);}Thread.sleep(1000 * 3);System.out.println("所有的Client都在等待");controlBarrier.removeBarrier();service.shutdown();service.awaitTermination(10, TimeUnit.MINUTES);client.close();System.out.println("OK!");}}
Client #1 等待Client #2 等待Client #0 等待Client #4 等待Client #3 等待所有的Client都在等待Client #4 开始Client #2 开始Client #0 开始Client #3 开始Client #1 开始OK!

2.双栅栏Double Barrier
// client - the client// barrierPath - path to use// memberQty - the number of members in the barrierpublic DistributedDoubleBarrier(CuratorFramework client, String barrierPath, int memberQty)
- enter()、enter(long maxWait, TimeUnit unit) - 等待同时进入栅栏
- leave()、leave(long maxWait, TimeUnit unit) - 等待同时离开栅栏
public class DistributedBarrierDoubleExample{private static final int QTY = 5;private static final String PATH = "/examples/barrier";public static void main(String[] args) throws Exception{CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));client.start();ExecutorService service = Executors.newFixedThreadPool(QTY);for (int i = 0; i < (QTY + 2); ++i){final DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, PATH, QTY);final int index = i;Callable<Void> task = new Callable<Void>(){@Overridepublic Void call() throws Exception{Thread.sleep((long) (3 * Math.random()));System.out.println("Client #" + index + " 等待");if(false == barrier.enter(5, TimeUnit.SECONDS)){System.out.println("Client #" + index + " 等待超时!");return null;}System.out.println("Client #" + index + " 进入");Thread.sleep((long) (3000 * Math.random()));barrier.leave();System.out.println("Client #" + index + " 结束");return null;}};service.submit(task);}service.shutdown();service.awaitTermination(10, TimeUnit.MINUTES);client.close();System.out.println("OK!");}}
Client #0 等待Client #2 等待Client #3 等待Client #4 等待Client #1 等待Client #4 进入Client #2 进入Client #0 进入Client #1 进入Client #3 进入Client #4 结束Client #5 等待Client #2 结束Client #3 结束Client #6 等待Client #0 结束Client #1 结束Client #5 等待超时!Client #6 等待超时!OK!

-------------------------------------------------------------------------------------------------------------------------------
06.Curator Barrier的更多相关文章
- [译]ZOOKEEPER RECIPES-Barriers
Barrier 在分布式系统中常使用Barrier来阻塞进程,当满足一定条件后再恢复进行后续操作.Barrier在Zookeeper中可以通过设计一个Barrier节点来实现.Barrier 节点存在 ...
- CuratorBarrier
一.DistributedDoubleBarrier 同时开始,同时结束 package bjsxt.curator.barrier; import java.util.Random; import ...
- 六、curator recipes之屏障barrier
简介 curator针对分布式场景实现了分布式屏障:barrier.我们在分布式系统中可以使用barrier去阻塞进程,知道某个条件被触发.其实跟Java多线程的barrier是一样的. 例如:当两个 ...
- Apache Curator获得真正的
Apache Curator获得真正的 Curator它是Netflix一家公司来源Zookeeper顾客,与Zookeeper相比于提供本地客户端,Curator的抽象层次更高,简化了Zookeep ...
- Apache Curator入门实战
Apache Curator入门实战 Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更高,简化了Zookeep ...
- Apache Curator: Zookeeper客户端
Apache Curator Framework url: http://curator.apache.org/curator-framework/ The Curator Framework is ...
- Zookeeper开源客户端框架Curator简介
Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情 ...
- 内存屏障(Memory barrier)-- 转发
本文例子均在 Linux(g++)下验证通过,CPU 为 X86-64 处理器架构.所有罗列的 Linux 内核代码也均在(或只在)X86-64 下有效. 本文首先通过范例(以及内核代码)来解释 Me ...
- Zookeeper开源客户端框架Curator简介[转]
Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情 ...
随机推荐
- Java数据结构和算法(三):常用排序算法与经典题型
常用的八种排序算法 1.直接插入排序 我们经常会到这样一类排序问题:把新的数据插入到已经排好的数据列中.将第一个数和第二个数排序,然后构成一个有序序列将第三个数插入进去,构成一个新的有序序列.对第四个 ...
- C++ 类的实例中 内存分配详解
一个类,有成员变量:静态与非静态之分:而成员函数有三种:静态的.非静态的.虚的. 那么这些个东西在内存中到底是如何分配的呢? 以一个例子来说明: #include"iostream.h&qu ...
- C++加载dll失败或显示乱码
右键项目-属性-字符集-使用多字节字符集
- 我的高效编程的秘诀--开发环境的重要性(IOS)
我觉得一个好的开发环境,能够让一个开发者的工作效率提高两倍以上,也能够让一个小白看上去不那么蠢: 开发环境不是仅仅安装一个xcode这种ide就OK了,在我看来开发环境的部署范围比較广泛,以下我来说说 ...
- CentOS6.x和CentOS7.X启动系统不显示进度条设置方法
[root@ok Desktop]# vi /boot/grub/grub.conf # grub.conf generated by anaconda # # Note that you do no ...
- ngBind {{}} ngBindTemplate
1.首先我们最常使用的一个绑定表达式的指令是ngBind,比如在一个div标签中我们可以这样使用: <div ng-bind="vm.info"></div> ...
- CentOS6.4下Docker应用环境的部署配置
http://blog.chinaunix.net/uid-619485-id-4973941.html *********************************************** ...
- Unix系统编程()信号类型和默认行为
信号类型和默认行为 就是讲了有多少个信号类型 好多啊,后面用到了再看...
- C语言 · 数组查找及替换
算法训练 数组查找及替换 时间限制:1.0s 内存限制:512.0MB 问题描述 给定某整数数组和某一整数b.要求删除数组中可以被b整除的所有元素,同时将该数组各元素按从小到大排序. ...
- linux内核对中断的处理方式
中断取代了轮询的通知方式,DMA取代了轮询的读写数据方式. 分类软件指令造成的中断(又叫异常,同步中断). svc, und, abt硬件通过中断请求信号造成的中断(异步中断). irq,fi ...