The Cyclicbarrier of Java concurrent programming

Source: Internet
Author: User

A synchronization helper class that allows a set of threads to wait for each other until a common barrier point (common barrier points) is reached. In programs that involve a set of fixed-size threads, these threads have to wait for each other, and cyclicbarrier is useful at this time. Because the barrier can be reused after releasing the waiting thread, it is called a cyclic barrier.


Cyclicbarrier supports an optional Runnable command that runs only once at each barrier point after the last thread in a set of threads arrives (but before releasing all threads). This barrier operation is useful if you update the shared state before continuing with all participating threads.

Countdownlatch: One thread (or more) waits for another n thread to complete something before it can be executed. Cyclicbarrier:n threads wait for each other, and all threads must wait before any one thread finishes.

This should be clear, for Countdownlatch, the focus is the "one thread", it is waiting, and the other n of the thread after the "something" can continue to wait, can be terminated. For Cyclicbarrier, the point is that n threads, either one of them is not finished, and all the threads must wait.


Package Com.lala.shop;import Java.util.random;import Java.util.concurrent.brokenbarrierexception;import Java.util.concurrent.cyclicbarrier;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import java.util.concurrent.timeunit;/** * Here is an example: five people to buy shirts, trousers, shoes together. * Everyone has to buy all the shirts before they can buy pants, all the pants, after the purchase of shoes, all the shoes finished, the thing is done */public class Cyclicbarrierdemo {public static void main ( String[] args) {Cyclicbarrier cb = new Cyclicbarrier (5, new Runnable () {public void run () {System.out.println ("People are already in alignment, ready for the next step ...");}}); Executorservice runner = Executors.newfixedthreadpool (5) Runner.submit (new shopping ("Li Mouth", CB)); Runner.submit (new Shopping ("Bai Zhantong", CB)), Runner.submit (New shopping ("Guofu", CB)), Runner.submit (New shopping ("Tong Xiang Yu", CB)); Runner.submit ( New Shopping ("Lu Xiu", CB)); Runner.shutdown ();}} Class Shopping implements Runnable{private string user;private cyclicbarrier cb;public Shopping (string user, Cyclicbarrier cb) {this.user = USER;THIS.CB = cb;} public void Run () {try {long shirtTime = Getrandomtime (); TimeUnit.SECONDS.sleep (Shirttime); System.out.println (user + "bought the shirt, took the time:" + shirttime); cb.await (); Long pantstime = Getrandomtime (); TimeUnit.SECONDS.sleep (Pantstime); System.out.println (user + "bought trousers, took time:" + pantstime); cb.await (); Long shosetime = Getrandomtime (); TimeUnit.SECONDS.sleep (Shosetime); System.out.println (user + "bought the shoes, took the time:" + shosetime); cb.await (); System.out.println (user + "Something has been bought, go Home");} catch (Interruptedexception | Brokenbarrierexception e) {e.printstacktrace ();}} Private Long Getrandomtime () {return new Random (). Nextint (9) + 1;}}


The output is:


Lu Xiu just bought a shirt and spent time: 1
Bai Zhantong bought a shirt, took time: 1
Shang bought a shirt, took time: 3
Li Mouth bought a shirt, took time: 8
Guofu bought a shirt, took time: 9
People are already there, ready for the next step ...
Bai Zhantong bought pants, took time: 4
Shang bought Pants, took time: 4
Lu Xiu just bought his pants and spent time: 5
Guofu bought Pants, took time: 8
Li Mouth bought pants, took time: 9
People are already there, ready for the next step ...
Lu Xiu just bought the shoes, took the time: 1
Bai Zhantong bought shoes, took time: 2
Shang bought shoes, took time: 7
Guofu bought shoes, took time: 8
Li Mouth bought shoes, took time: 8
People are already there, ready for the next step ...
Li mouth things have been bought, go home
Lu Xiu has been bought and gone home.
Bai Zhantong things have been bought and gone home
Guofu things have been bought and gone home
Shang things have been bought and gone home

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The Cyclicbarrier of Java concurrent programming

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.