Zookeeper实例Curator API-分布式Barrier
来源:互联网 发布:jvm调优面试题知乎 编辑:程序博客网 时间:2024/06/06 23:51
使用CyclicBarrier模拟龟兔赛跑
package test_test.test_haha;import java.io.IOException;import java.util.concurrent.CyclicBarrier;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * * @ClassName: Recipes_CyclicBarrier * @Description: TODO(使用CyclicBarrier模拟龟兔赛跑) * @author RongShu* @date 2017年6月17日 下午2:13:03 * */public class Recipes_CyclicBarrier {public static CyclicBarrier barrier = new CyclicBarrier( 3 );public static void main( String[] args ) throws IOException, InterruptedException {ExecutorService executor = Executors.newFixedThreadPool( 3 );executor.submit( new Thread( new Runner( "1号选手" ) ) );executor.submit( new Thread( new Runner( "2号选手" ) ) );executor.submit( new Thread( new Runner( "3号选手" ) ) );executor.shutdown();}}class Runner implements Runnable {private String name;public Runner( String name ) {this.name = name;}public void run() {System.out.println( name + " 准备好了." );try {Recipes_CyclicBarrier.barrier.await();} catch ( Exception e ) {}System.out.println( name + " 起跑!" );}}输出2号选手 准备好了.1号选手 准备好了.3号选手 准备好了.3号选手 起跑!2号选手 起跑!1号选手 起跑!
方式1
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;/** * * @ClassName: Recipes_Barrier * @Description: TODO(使用Curator实现分布式Barrier方式一) * @author RongShu * @date 2017年6月17日 下午2:10:32 * */public class Recipes_Barrier {static String barrier_path = "/curator_recipes_barrier_path";static DistributedBarrier barrier;public static void main(String[] args) throws Exception {for (int i = 0; i < 5; i++) {new Thread(new Runnable() {public void run() {try {CuratorFramework client = CuratorFrameworkFactory.builder().connectString("localhost:2181").retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();client.start();barrier = new DistributedBarrier(client, barrier_path);System.out.println(Thread.currentThread().getName() + "号barrier设置");barrier.setBarrier();barrier.waitOnBarrier();System.err.println("启动...");} catch (Exception e) {}}}).start();}Thread.sleep(2000);barrier.removeBarrier();}}输出Thread-4号barrier设置Thread-0号barrier设置Thread-3号barrier设置Thread-2号barrier设置Thread-1号barrier设置启动...启动...启动...启动...启动...
方式2
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;public class Recipes_Barrier2 {static String barrier_path = "/curator_recipes_barrier_path";public static void main(String[] args) throws Exception {for (int i = 0; i < 5; i++) {new Thread(new Runnable() {public void run() {try {CuratorFramework client = CuratorFrameworkFactory.builder().connectString("localhost:2181").retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();client.start();DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, barrier_path, 5);Thread.sleep(Math.round(Math.random() * 3000));System.out.println(Thread.currentThread().getName() + "号进入barrier");barrier.enter();System.out.println("启动...");Thread.sleep(Math.round(Math.random() * 3000));barrier.leave();System.out.println("退出...");} catch (Exception e) {}}}).start();}}}输出Thread-4号进入barrierThread-3号进入barrierThread-1号进入barrierThread-2号进入barrierThread-0号进入barrier启动...启动...启动...启动...启动...退出...退出...退出...退出...退出...
参考
1.《从Paxos到Zookeeper:分布式一致性原理与实践》
2.https://zookeeper.apache.org/doc/r3.5.3-beta/javaExample.html
阅读全文
0 0
- Zookeeper实例Curator API-分布式Barrier
- Zookeeper实例Curator API-分布式锁
- Zookeeper实例Curator API-分布式计数器
- Zookeeper实例Curator API-PathChildrenCache
- Zookeeper实例Curator API-PathChildrenCache_NoCacheData
- Zookeeper实例Curator API-TestingServer
- Zookeeper实例Curator API-TestingCluster
- Zookeeper实例Curator API-NodeCache使用实例
- Zookeeper实例Curator API-Master选举
- Zookeeper实例Curator API-ZKPaths使用
- Zookeeper实例Curator API-EnsurePath使用
- Zookeeper实例Curator API-使用Curator创建节点
- Zookeeper实例Curator API-使用Curator删除节点
- Zookeeper实例Curator API-使用Curator获取数据内容
- Zookeeper实例Curator API-使用Curator更新数据内容
- Zookeeper实例Curator API-使用Curator的异步接口
- Curator典型使用场景之分布式Barrier。
- Zookeeper实例Curator API-NodeCache节点不存在实例
- 使用ssh连接docker容器
- Json转Map
- ubuntu16.04 远程控制win10
- git-2.10.2-64-bit安装教程
- shape的使用并完成按钮机制
- Zookeeper实例Curator API-分布式Barrier
- Thinkphp5学习(24)模型输出
- 几种控件的使用代码
- Linux
- 文件的保存与删除
- Android Tv HDMI-CEC框架层移植
- catch exception when loading tinker:java.lang.IllegalAccessError
- Eclipse 的快捷键以及文档注释、多行注释的快捷键
- 高性能Mysql 2(性能优化)