Zookeeper Study notes (vi) distributed barrier

Source: Internet
Author: User
Tags reserved sleep zookeeper

When we need multiple threads to start at the same time, we can use the cyclibarrier that comes with the JDK. Let's take a look at its common uses:

Package com.my.CuratorTest;
Import java.util.concurrent.BrokenBarrierException;

Import Java.util.concurrent.CyclicBarrier; /** * Title: <br/> * Intention: <br/> * <p> * Class name:com.my.curatortest.cyclibarriertest<br/ > * Create date:2017/8/26 11:18 <br/> * Project name:mytest <br/> * company:all rights Reserved. <br/> * copyright©2017 <br/> * </p> * <p> * Author:gaowei <br/> * 1st_examiner: < br/> * 2nd_examiner: <br/> * </p> * * @version 1.0 * @since JDK 1.7 */public class Cyclibarriertest
		{public static void main (string[] args) {cyclicbarrier barrier = new Cyclicbarrier (5); for (int i = 0; i < 5; i++) {New Thread (new Runnable () {@Override public void run () {System.out.prin
					TLN (Thread.CurrentThread (). GetName () + ", waiting ...");
						try {barrier.await (); System.out.println (Thread.CurrentThread (). GetName () + "," + System.currenttimemillis() + ", start work");
					} catch (Interruptedexception e) {e.printstacktrace ();
					} catch (Brokenbarrierexception e) {e.printstacktrace ();
		}}). Start (); }
	}
}

Operation Result:

Thread-3, wait ...
Thread-1, wait ...
Thread-0, wait ...
Thread-2, wait ...
Thread-4, wait ...
Thread-4, 1503736464892, started working
Thread-3, 1503736464892, started working
Thread-0, 1503736464892, started working
Thread-1, 1503736464892, start work
Thread-2, 1503736464892, start working

So, in a distributed environment, how to solve the problem of multiple threads starting at the same time. Curator provides us with distributedbarrier (main thread triggering barrier release) and distributeddoublebarrier (thread spontaneous triggering barrier release).

Distributedbarrier is used as follows, copy the code on the book:

Package com.my.CuratorTest;
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; /** * Title: <br/> * Intention: <br/> * <p> * Class NAME:COM.MY.CURATORTEST.RECIPESBARRIER&LT;BR/&G
 T * Create DATE:2017/8/26 11:07 <br/> * Project name:mytest <br/> * company:all rights Reserved. <br/> * copyright©2017 <br/> * </p> * <p> * Author:gaowei <br/> * 1st_examiner: <

	br/> * 2nd_examiner: <br/> * </p> * * @version 1.0 * @since JDK 1.7 */public class Recipesbarrier {
	static String Barrierpath = "/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 () {@ Override Public void Run () {curatorframework client = Curatorframeworkfactory.builder (). ConnectString ("127.0.0.1:2181")
					. Retrypolicy (New Exponentialbackoffretry (8)). Build ();
					Client.start ();
					Barrier = new Distributedbarrier (client, Barrierpath);
					System.out.println (Thread.CurrentThread (). GetName () + "number barrier settings");
						try {barrier.setbarrier ();
						Barrier.waitonbarrier ();
					System.err.println (Thread.CurrentThread (). GetName () + "start ...");
					} catch (Exception e) {e.printstacktrace ();
		}}). Start ();
		} thread.sleep (2000);
	Barrier.removebarrier (); }
}

Operation Result:

Thread-4 number Barrier set
Thread-2 number Barrier set Thread-3 number Barrier set Thread-0 number Barrier set Thread-1
number barrier
settings
Thread-3 start ...
Thread-4 start ...
Thread-2 start ...
Thread-0 start ...
Thread-1 start ...

The use of Distributeddoublebarrier is as follows:

Package com.my.CuratorTest;
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; /** * Title: <br/> * Intention: <br/> * <p> * Class name:com.my.curatortest.recipesbarrier2<br/&
 Gt * Create DATE:2017/8/26 16:05 <br/> * Project name:mytest <br/> * company:all rights Reserved. <br/> * copyright©2017 <br/> * </p> * <p> * Author:gaowei <br/> * 1st_examiner: < 

	br/> * 2nd_examiner: <br/> * </p> * * @version 1.0 * @since JDK 1.7 */public class RecipesBarrier2 {

	static String Barrierpath = "/curator_recipes_barrier_path";
				public static void Main (string[] args) {for (int i = 0; i < 5; i + +) {new Thread (new Runnable () {@Override public void Run () {try {CuratoRframework client = Curatorframeworkfactory.builder (). ConnectString ("127.0.0.1:2181"). Retrypolicy (New Ex
						Ponentialbackoffretry (3)). Build ();
						Client.start ();
						Distributeddoublebarrier barrier = new Distributeddoublebarrier (client, Barrierpath, 5);
						Thread.Sleep (Math.Round (Math.random () * 3000));
						System.out.println (Thread.CurrentThread (). GetName () + "sign into barrier");
						Barrier.enter ();
						System.out.println (Thread.CurrentThread (). GetName () + "start ...");
						Thread.Sleep (Math.Round (Math.random () * 3000));
						Barrier.leave ();
					System.out.println (Thread.CurrentThread (). GetName () + "Exit ...");
					} catch (Interruptedexception e) {e.printstacktrace ();
					} catch (Exception e) {e.printstacktrace ();
		}}). Start (); }
	}
}

Operation Result:

Thread-2 Enter barrier
Thread-1 Enter barrier Thread-4 enter
barrier Thread-0 Enter barrier Thread-3 enter barrier
Thread-3 start ...
Thread-4 start ...
Thread-1 start ...
Thread-0 start ...
Thread-2 start ...
Thread-2 exit ...
Thread-4 exit ...
Thread-0 exit ...
Thread-1 exit ...
Thread-3 exit ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.