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自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情 ...
随机推荐
- mysql innodb的重要组件
innodb包涵如下几个组件 一.innodb_buffer_pool: 1 它主要用来缓存数据与索引(准确的讲由于innodb中的表是由聚集索引组织的,所以数据只不是过主键这个索引的叶子结点). 二 ...
- android studio 使用问题记录
android studio 使用问题记录 下载地址:FQ或园子内好心人提供的国内地址: http://www.cnblogs.com/bjzhanghao/archive/2012/11/14/an ...
- svn move (mv,rename,ren)
svn 重命名文件: [root@NGINX-APACHE-SVN pro]# svn move 20160624新建数据库表.txt 201.txt A 201.txt D 20160624新建数据 ...
- ENGINE_API CXNoTouch
/************************************************************************/ //屏蔽消息面板 //优先级默认为 TP_BOTT ...
- PyCharm 在django程序中单独运行py文件
使用PyCharm开发django程序,发现如果不在命令行而在IDE的django项目中直接运行django程序,发现报错,程序如下: def main(): from people.models ...
- HDFS设计思路,HDFS使用,查看集群状态,HDFS,HDFS上传文件,HDFS下载文件,yarn web管理界面信息查看,运行一个mapreduce程序,mapreduce的demo
26 集群使用初步 HDFS的设计思路 l 设计思想 分而治之:将大文件.大批量文件,分布式存放在大量服务器上,以便于采取分而治之的方式对海量数据进行运算分析: l 在大数据系统中作用: 为各类分布式 ...
- 巧用set比较大小,缩短时间复杂度
struct Node { long long a; long long b; long long c; long long num; int i; bool operator < (const ...
- Wireshark-TCP协议分析(包结构以及连接的建立和释放)
原文:http://blog.csdn.net/ahafg/article/details/51039584 TCP:传输控制协议 TCP是一种面向连接的.可靠的.基于字节流的传输层通信协议. 面向 ...
- SVN文件排除
背景:原来SVN库人事2.0在24.42server上,后来server改革,把库迁移到了24.248server上. 原来24.42上的库,在update或commit后文件总是绿色的.看着心里特别 ...
- [Linux]Linux printf 输出重定向
方法一 #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <sys/st ...