curator之recipes之Barrier
来源:互联网 发布:it专业学什么 编辑:程序博客网 时间:2024/05/17 05:51
参考文档: http://ifeve.com/zookeeper-barrier/
分布式系统中使用barrier阻塞多个进程,直到某个条件满足时,所有节点上的进行才会继续执行。
比如赛马比赛中, 等赛马陆续来到起跑线前。 一声令下,所有的赛马都飞奔而出。
栅栏Barrier
相关的类
- DistributedBarrier
使用
public DistributedBarrier(CuratorFramework client, String barrierPath)Parameters:client - clientbarrierPath - path to use as the barrier
首先需要设置栅栏,它将阻塞在它上面等待的线程:
setBarrier();
然后需要阻塞的线程调用“方法等待放行条件:
public void waitOnBarrier()
当条件满足时,移除栅栏,所有等待的线程将继续执行:
removeBarrier();
异常处理
DistributedBarrier 会监控连接状态,当连接断掉时waitOnBarrier()方法会抛出异常。
代码示例
package barrier;import java.util.concurrent.Callable;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;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;import org.apache.curator.test.TestingServer;public class DistributeBarrierExample {private static final int QTY=5;private static final String PATH="/examples/barrier";public static void main(String[] args){ TestingServer server=null; try { server=new TestingServer(); CuratorFramework client=CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(100,3)); client.start(); ExecutorService pool=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>(){ @Override public Void call() throws Exception { Thread.sleep((long) (3 * Math.random())); System.out.println("Client #" + index + " waits on Barrier"); barrier.waitOnBarrier(); System.out.println("Client #" + index + " begins"); return null; } }; pool.submit(task); } Thread.sleep(10000); System.out.println("all Barrier instances should wait the condition"); controlBarrier.removeBarrier(); pool.shutdown(); pool.awaitTermination(10, TimeUnit.MINUTES); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }}}
这个例子创建了controlBarrier来设置栅栏和移除栅栏。 我们创建了5个线程,在此Barrier上等待。 最后移除栅栏后所有的线程才继续执行。
如果你开始不设置栅栏,所有的线程就不会阻塞住。
双栅栏Double Barrier
双栅栏允许客户端在计算的开始和结束时同步。当足够的进程加入到双栅栏时,进程开始计算, 当计算完成时,离开栅栏。
相关的类
- DistributedDoubleBarrier
使用
新建实例
public DistributedDoubleBarrier(CuratorFramework client, String barrierPath, int memberQty)Creates the barrier abstraction. memberQty is the number of members in the barrier. When enter() is called, it blocks untilall members have entered. When leave() is called, it blocks until all members have left.Parameters:client - the clientbarrierPath - path to usememberQty - the number of members in the barrier
memberQty是成员数量,当enter方法被调用时,成员被阻塞,直到所有的成员都调用了enter。 当leave方法被调用时,它也阻塞调用线程, 知道所有的成员都调用了leave。 就像百米赛跑比赛, 发令枪响, 所有的运动员开始跑,等所有的运动员跑过终点线,比赛才结束。
DistributedBarrier 会监控连接状态,当连接断掉时enter()和leave方法会抛出异常。
代码示例
package barrier;import java.util.concurrent.Callable;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.framework.recipes.barriers.DistributedDoubleBarrier;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.curator.test.TestingServer;public class DistributedBarrierExample { private static final int QTY = 5; private static final String PATH = "/examples/barrier"; public static void main(String[] args) throws Exception { try (TestingServer server = new TestingServer()) { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); client.start(); ExecutorService service = Executors.newFixedThreadPool(QTY); for (int i = 0; i < QTY; ++i) { final DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, PATH, QTY); final int index = i; Callable<Void> task = new Callable<Void>() { @Override public Void call() throws Exception { Thread.sleep((long) (3 * Math.random())); System.out.println("Client #" + index + " enters"); barrier.enter(); System.out.println("Client #" + index + " begins"); Thread.sleep((long) (3000 * Math.random())); barrier.leave(); System.out.println("Client #" + index + " left"); return null; } }; service.submit(task); } service.shutdown(); service.awaitTermination(10, TimeUnit.MINUTES); } }}
0 0
- curator之recipes之Barrier
- Curator之Recipes之选举
- Curator之Recipes之锁
- Curator之recipes之Counters
- Curator之recipes之Cache
- Curator典型使用场景之分布式Barrier。
- Curator Recipes
- C++ 之 Memory Barrier
- 线程同步之 barrier
- GCD之barrier
- Apache curator-recipes代码范例
- zookeeper之curator
- Curator之Leader选举
- Curator之创建节点。
- Curator之删除节点。
- Curator之读取数据。
- Curator之更新数据。
- Curator之异步接口。
- AT+CNMI新消息提示
- 安卓自定义时间轴
- P/Invoke (平台调用)
- 使用面向切面编程统计流量aop
- Hadoop YARN中内存的设置
- curator之recipes之Barrier
- 欢迎使用CSDN-markdown编辑器
- Xcode因为证书问题经常报的那些错
- 点击<a>标签,禁止页面自动跳到顶部的解决办法
- Xcode 插件更新
- HTML 中的块级元素和内联元素
- jquery-mobile一直加载的原因
- Lucene学习(二):Lucene的总体架构
- JSP基础:JSP九大内置对象