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自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情 ...
随机推荐
- C#趣味程序---爱因斯坦的台阶问题
问题:设有一阶梯,每步跨2阶.最后余1阶.每步跨3阶.最后余2阶:每步跨5阶.最后余4阶:每步跨6阶.最后余5阶:每步跨7阶.刚好到阶顶.问共同拥有多少阶梯? using System; namesp ...
- android.animation(6) - AnimatorSet
上几篇给大家分别讲了ValueAnimator和ObjectAnimator,相比而言ObjectAnimator更为方便而且由于set函数是在控件类内部实现,所以封装性更好.而且在现实使用中一般而言 ...
- Nmap速查手册
http://drops.wooyun.org/tips/4333 From:http://highon.coffee/docs/nmap/ 0x00:说明 只是一个快速查询手册,理论的东西都没有补充 ...
- Django教程:[33]从数据库生成模型
在使用django做网站的时候,有时候我们的数据库来自一个已有的数据库,如何整合这个数据库呢? django提供了方便的方法来整合已有数据库,下面我们看看具体的方法: 1.先来设置数据库:在网站文件夹 ...
- jquery 取第一个兄弟节点
1.HTML <table> <tr> <td>1</td> <td>abc</td> <td>def</td ...
- 第八课:不一样的链表 linux链表设计哲学 5星级教程
这一课最后实现的链表,和普通链表不同,借鉴了linux内核链表的思想,这也是企业使用的链表. 基础介绍: 顺序表的思考 顺序表的最大问题是插入和删除需要移动大量的元素!如何解决?A:在线性表数据元素之 ...
- EF调用存储过程、函数
一.ef4.1 codeFirst 修改表结构 增加字段等 EF code first需要重新生成库导致数据丢失的问题 说这个问题前 首先先说下 我使用ef4.1 codefirst的目的. 是因为可 ...
- Python中import和from import
Python里面的import和from import都是用于导入一个模块,两者的区别是 如果你在使用某模块内函数时不想写模块名,那么就用from import方式导入,如果是用import方式就要写 ...
- 机器学习:如何通过Python入门机器学习
我们都知道机器学习是一门综合性极强的研究课题,对数学知识要求很高.因此,对于非学术研究专业的程序员,如果希望能入门机器学习,最好的方向还是从实践触发. 我了解到Python的生态对入门机器学习很有帮助 ...
- mysql中创建用户和赋权限
mysql命令行用的不多,大部分使用工具类替代,所以这里记录下命令行模式下创建用户和赋予权限的命令,不用每次麻烦百度. 1. 创建oozie用户,%符号表示仅限于远程登录 create user 'o ...