跟着实例学习ZooKeeper的用法: Barrier
来源:互联网 发布:小学数学题软件下载 编辑:程序博客网 时间:2024/06/07 03:35
分布式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 com.colobu.zkrecipe.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 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); 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; } }; service.submit(task); } Thread.sleep(10000); System.out.println("all Barrier instances should wait the condition"); controlBarrier.removeBarrier(); service.shutdown(); service.awaitTermination(10, TimeUnit.MINUTES); } }}
这个例子创建了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
方法会抛出异常。
公众号推荐:
公众号:VOA英语每日一听
微信号: voahk01
可长按扫码关注,谢谢
例子代码:
package com.colobu.zkrecipe.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.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); } }}
- 跟着实例学习ZooKeeper的用法: Barrier
- 跟着实例学习ZooKeeper的用法: Barrier
- 跟着实例学习ZooKeeper的用法: 计数器
- 跟着实例学习ZooKeeper的用法: 队列
- 跟着实例学习ZooKeeper的用法: 缓存
- 跟着实例学习ZooKeeper的用法: 计数器
- 跟着实例学习ZooKeeper的用法: 队列
- 跟着实例学习ZooKeeper的用法: 计数器
- 跟着实例学习ZooKeeper的用法: 缓存
- 跟着实例学习ZooKeeper的用法: Leader选举
- 跟着实例学习ZooKeeper的用法: 临时节点
- 跟着实例学习ZooKeeper的用法: 分布式锁
- 跟着实例学习ZooKeeper的用法: Leader选举
- 跟着实例学习ZooKeeper的用法: Curator框架应用
- 跟着实例学习ZooKeeper的用法: 文章汇总
- 跟着实例学习ZooKeeper的用法: Leader选举
- 跟着实例学习ZooKeeper的用法: 临时节点
- 跟着实例学习ZooKeeper的用法: 分布式锁
- Java类与类之间的关系
- OGG_GoldenGate复杂参数定义(案例)
- RFID 知识的学习
- 未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载
- 整数相除给double赋值问题
- 跟着实例学习ZooKeeper的用法: Barrier
- Spring Cloud构建微服务架构—服务消费Feign
- linux SVN命令
- 腾讯225页报告:AI正取代互联网成为新引擎,马化腾提七大关键词
- java 修改后台文件名
- 【第六届蓝桥杯】格子中输出
- 安迪-比尔定律的影响
- cloudera-scm-server-db 启动失败
- Eclipse的界面风格