Usage of cyclicbarrier in Java Java, example explained

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.

The previous program was implemented with Countdownlatch, and now try to rewrite it with Cyclicbarrier.

Package Thread.thread;import Java.util.concurrent.countdownlatch;import Java.util.concurrent.cyclicbarrier;import Java.util.concurrent.linkedblockingdeque;import Java.util.concurrent.threadpoolexecutor;import java.util.concurrent.timeunit;/** * classname:countdownlatchtest <br/> * Function:todo ADD Function. <br/> * Reason:todo ADD Reason.  <br/> * date:2015 July 30 pm 2:04:07 <br/> * * @author Chiwei * @version * @since JDK 1.6 * @see */public class  cyclicbarriertest {public static void main (string[] args) {threadpoolexecutor poolexe = new Threadpoolexecutor (100, 1000,  1, Timeunit.seconds, new linkedblockingdeque<runnable>); int count = 10;//The test starts ringing and the exam begins final Countdownlatch  Exambegin = new Countdownlatch (1);//single candidate, final exam completed cyclicbarrier student = new Cyclicbarrier (count+1);//One Examination Room 10 candidates for (int i = 0; i < count; i++) {Runnable Runnable = new Runnable () {public void run () {try {System.out.println ("candidate" + Thread.CurrentThread (). GetName () + "In suchThe bell that is about to start the test rings "); exambegin.await (); System.out.println ("candidate hears ringtones" + thread.currentthread (). GetName () + "start Answer"); Thread.Sleep ((Long) (Math.random () * 100));//answer process, real business logic processing part SYSTEM.OUT.PRINTLN ("examinee" + thread.currentthread (). GetName () + "handing in"); Student.await ();} catch (Exception e) {e.printstacktrace ();}}}; Poolexe.execute (runnable); Athlete starts task}try {//Answer time Thread.Sleep ((Long) (Math.random () * 10000)); SYSTEM.OUT.PRINTLN ("Examination room" + thread.currentthread (). GetName () + "Start ringtone is about to ring"); Exambegin.countdown (); SYSTEM.OUT.PRINTLN ("Examination room" + thread.currentthread (). GetName () + "Test starts ringing"); student.await (); All candidates are handed in System.out.println ("Examination room" + thread.currentthread (). GetName () + "End of Exam");} catch (Exception e) {e.printstacktrace ();} Poolexe.shutdown ();}}
The parameter count value of the Countdownlatch constructor method is the number of calls to Countdown, and the number of cyclicbarrier is await, so add 1

Examinee pool-1-thread-1 waiting for the test to start the bell sounded examinee pool-1-thread-2 waiting for the test to start the bell sounded examinee pool-1-thread-6 waiting for the test to start the bell sounded examinee pool-1-thread-7 waiting for the test to start the bell sounded examinee poo L-1-thread-5 waiting for the test to start the bell sounded examinee pool-1-thread-4 waiting for the test to start the bell sounded examinee pool-1-thread-10 waiting for the test to start the bell sounded examinee pool-1-thread-9 waiting for the test to start the bell sounded examinee pool-1- Thread-3 waiting for the test to start the bell rang test taker pool-1-thread-8 waiting for the test to start the bell rang the examination room main starts ringing the examination room main test start bell sounded examinee hear ringtone pool-1-thread-1 start answer test taker hear ringtone pool-1-thread -2 Start answer the test taker hears the ringtone pool-1-thread-6 begins to answer the test taker hears the bell pool-1-thread-7 starts to answer the test taker hears the ringtone pool-1-thread-5 starts to answer the test taker hears the ringtone pool-1-thread-4 begins to answer the Examinee hears the ringtone Read-3 begin to answer the test taker hears the Bell pool-1-thread-8 start answering the test taker hears the bell pool-1-thread-9 starts to answer the examinee hears the ringtone pool-1-thread-10 starts to answer the examinee pool-1-thread-5 to test the examinee pool-1-thread- 4 to test the examinee pool-1-thread-9 to test the examinee pool-1-thread-2 to test the examinee pool-1-thread-6 handed in the examinee Pool-1-thread-1 handed in the examinee pool-1-thread-10 test taker pool-1-thread-7 test taker P Ool-1-thread-8 test Taker POOL-1-THREAD-3 Examination exam completed main exams
From the results, the same effect is achieved with countdownlatch.

Note the difference between the two:

Countdownlatch is a countdown that fires an event after multiple threads have executed, wakes up the thread that is waiting on the latch, and executes the countdown thread, continuing to work on its own thread after the countdown is done;

Cyclicbarrier is a fence that synchronizes all the threads that call the await method, and when all the threads are in the await method, they return together to continue their work, and the outermost of an await is counted to 0, and all threads continue to execute. Because threads using Cyclicbarrier are blocked on the await method, use caution in the thread pool, and deadlock occurs if the thread pool has too few threads

Student.await (); SYSTEM.OUT.PRINTLN ("see if Cyclicbarrier's await method can block me!") ");
Examinee pool-1-thread-1 waiting for the test to start the bell sounded examinee pool-1-thread-3 waiting for the test to start the bell sounded examinee pool-1-thread-2 waiting for the test to start the bell sounded examinee pool-1-thread-4 waiting for the test to start the bell sounded examinee poo L-1-thread-5 waiting for the test to start the bell sounded examinee pool-1-thread-6 waiting for the test to start the bell sounded examinee pool-1-thread-7 waiting for the test to start the bell sounded examinee pool-1-thread-8 waiting for the test to start the bell sounded examinee pool-1-t Hread-9 waiting for the test to start the bell rang test taker pool-1-thread-10 waiting for the test to start the bell rang the examination room main starts ringing the examination room main test start bell sounded examinee hear ringtone pool-1-thread-1 start answer test taker hear ringtone pool-1-thread -2 Start answer the test taker hears the ringtone pool-1-thread-5 begins to answer the test taker hears the bell pool-1-thread-7 starts to answer the test taker hears the ringtone pool-1-thread-10 starts to answer the test taker hears the ringtone pool-1-thread-3 begins to answer the Examinee hears the ringtone Hread-9 begin to answer the test taker hears the Bell pool-1-thread-8 start answering the test taker hears the bell pool-1-thread-6 starts to answer the examinee hears the ringtone pool-1-thread-4 starts to answer the examinee pool-1-thread-2 to test the examinee pool-1-thread- 10 to test the examinee pool-1-thread-1 to test the examinee pool-1-thread-3 to test the examinee pool-1-thread-5 handed in the examinee pool-1-thread-4 handed in the examinee pool-1-thread-8 test taker pool-1-thread-7 test taker P Ool-1-thread-6 test taker pool-1-thread-9 hand in to see Cyclicbarrier await method can block me! Examination Hall main exam end See Cyclicbarrier's await method can block me! See if Cyclicbarrier's await method can block me! See if Cyclicbarrier's await method can block me! See if Cyclicbarrier's await method can block me! See if Cyclicbarrier's await method can block me! See if Cyclicbarrier's await method can block me! Look at Cyc.Licbarrier's await method can block me! See if Cyclicbarrier's await method can block me! See if Cyclicbarrier's await method can block me!
As a result, Cyclicbarrier's await method does block the continuation of the current thread, and only counts to 0 to continue execution.
Student.countdown (); SYSTEM.OUT.PRINTLN ("See if Countdownlatch's Countdown method can clog me up!") ");
Examinee pool-1-thread-1 waiting for the test to start the bell sounded examinee pool-1-thread-3 waiting for the test to start the bell sounded examinee pool-1-thread-2 waiting for the test to start the bell sounded examinee pool-1-thread-4 waiting for the test to start the bell sounded examinee poo L-1-thread-5 waiting for the test to start the bell sounded examinee pool-1-thread-6 waiting for the test to start the bell sounded examinee pool-1-thread-9 waiting for the test to start the bell sounded examinee pool-1-thread-8 waiting for the test to start the bell sounded examinee pool-1-t Hread-7 waiting for the test to start the bell rang test taker pool-1-thread-10 waiting for the test to start the bell rang the examination room main starts ringing the examination room main test start bell sounded examinee hear ringtone pool-1-thread-3 start answer test taker hear ringtone pool-1-thread -2 Start answer the test taker hears the ringtone pool-1-thread-1 begins to answer the examinee pool-1-thread-2 to test the examinee hears the bell pool-1-thread-10 starts to answer the examinee hears the ringtone pool-1-thread-7 starts to answer the examinee hears the ringtone pool-1-thread- 8 start answer the examinee hears the ringtone pool-1-thread-9 starts to answer the examinee hears the ringtone pool-1-thread-6 starts the answer question Examinee hears the ringtone pool-1-thread-5 starts to answer the examinee hears the ringtone pool-1-thread-4 starts to answer the examinee pool-1-thread- 9 hand over to see if Countdownlatch's Countdown method can block me! See if Countdownlatch's Countdown method can clog me up! Examinee pool-1-thread-10 hand over to see Countdownlatch countdown method can block me! Examinee pool-1-thread-7 hand over to see Countdownlatch countdown method can block me! Examinee pool-1-thread-4 hand over to see Countdownlatch countdown method can block me! Examinee pool-1-thread-3 hand over to see Countdownlatch countdown method can block me! Examinee pool-1-thread-5 hand over to see Countdownlatch countdown method can block me! Examinee pool-1-thread-6 to check Countdownlatch CThe Ountdown method can block me! Examinee pool-1-thread-1 hand over to see Countdownlatch countdown method can block me! Examinee pool-1-thread-8 hand over to see Countdownlatch countdown method can block me! Examination Hall Main exam End
While the Countdownlatch countdown method executes, the thread will continue to execute without blocking.



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

Usage of cyclicbarrier in Java Java, example explained

Related Article

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.