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自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情 ...
随机推荐
- PHP设计模式系列 - 观察者模式处理订单(异步操作附加功能)
观察者模式 观察者设计模式能够更便利创建和查看目标对象状态的对象,并且提供和核心对象非耦合的置顶功能性.观察者设计模式非常常用,在一般复杂的WEB系统中,观察者模式可以帮你减轻代码设计的压力,降低代码 ...
- Atitit.软件GUI按钮与仪表盘(01)--报警系统--
Atitit.软件GUI按钮与仪表盘(01)--报警系统-- 1. 温度报警区(鲁大师,360taskman) 1 2. os-区-----cpu_mem_io资源占用监测 1 3. Vm区 1 4. ...
- 云中的机器学习:FPGA 上的深度神经网络
人工智能正在经历一场变革,这要得益于机器学习的快速进步.在机器学习领域,人们正对一类名为“深度学习”算法产生浓厚的兴趣,因为这类算法具有出色的大数据集性能.在深度学习中,机器可以在监督或不受监督的方式 ...
- X86平台简称
1.PCH:PCH全称为Platform Controller Hub,是intel公司的集成南桥.AMD SB700/710/750 http://support.amd.com/TechDocs ...
- 从零开始学习OpenCL开发(一)架构
1 异构计算.GPGPU与OpenCL OpenCL是当前一个通用的由很多公司和组织共同发起的多CPU\GPU\其他芯片 异构计算(heterogeneous)的标准,它是跨平台的.旨在充分利用GPU ...
- [na]tcp的可靠性
- iOS7 Xcode 5如何设置隐藏状态栏
转自:http://www.cocoachina.com/ask/questions/show/99658 最简单直接的方法: 直接在RootViewController.mm里面(Cocos2d-x ...
- iOS字符串安全
iOS字符串安全 一个编译成功的可执行程序,其中已初始化的字符串都是完整可见的. 针对于iOS的Mach-O二进制通常可获得以下几种字符串信息: 资源文件名 可见的函数符号名 SQL语句 format ...
- html-blogsdemo
博客标题小样,代码预览是有动态效果的,但在博客园发布就没动画了,知道的大神麻烦告知下,谢谢. code <!DOCTYPE html> <html lang="en&quo ...
- VS2015配置Linux开发远程调试
# VS2015配置Linux开发远程调试 ### 简介-----------------------------vs2015支持跨平台开发 ### 软件环境--------------------- ...