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




原创粉丝点击