How to use Cyclicbarrier in "Java" Java, illustrative examples

Source: Internet
Author: User


A synchronization helper class that agrees that a group of threads waits for each other until it reaches a common barrier point (common barrier points). In programs that involve a set of fixed-size threads, these threads have to wait for each other at a time. Cyclicbarrier is very practical at this time.

Since 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. The exam begins final countdownlatch exambegin = new Countdownlatch (1);//single candidate, final exam completed cyclicbarrier student = new Cyclicbarrier ( COUNT+1);//An Examination Room 10 candidates for (int i = 0; i < count; i++) {Runnable Runnable = new Runnable () {public void run () {try {Syste M.out.println ("Candidate" + Thread.CurrentThread (). GetName () + "in theThe bell that starts at the beginning of the exam 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); The athlete starts the 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 () + "The 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 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 exam starts ringing the examinee hears the ringtone pool-1-thread-1 start answer the examinee hears the ringtone pool-1-thread -2 Start answer the test taker hears the ringtone pool-1-thread-6 start answer the examinee hears the bell pool-1-thread-7 starts the answer question the examinee hears the ringtone pool-1-thread-5 to start the answer the examinee hears the ringtone pool-1-thread-4 starts the question examinee hears the ringtone pool-1-th Read-3 start answer the test taker hears the bell pool-1-thread-8 starts the answer the examinee hears the Bell pool-1-thread-9 to start the question Examinee hears the ringtone pool-1-thread-10 starts the question 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
Judging from the results. and Countdownlatch achieve the same effect.



Note the difference between the two:

Countdownlatch triggers an event after multiple threads are running countdown, waking up the thread that is waiting on latch. The thread that runs countdown continues to work on its own thread after the countdown has run out.

Cyclicbarrier is a fence. The thread used to synchronize all calls to the await method. And when all the threads are up to the await method, they go back together to continue their work, and the outermost of an await is counted to 0, which is the whole thread that continues to run. Because threads that use Cyclicbarrier are blocked on the await method, use caution in thread pooling, assuming that the thread pool has too many threads. There's going to be a deadlock.

Student.await (); SYSTEM.OUT.PRINTLN ("see if Cyclicbarrier's await 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-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 exam starts ringing the examinee hears the ringtone pool-1-thread-1 start answer the examinee hears the ringtone pool-1-thread -2 Start answer the test taker hears the ringtone pool-1-thread-5 start answer the examinee hears the bell pool-1-thread-7 starts the answer question the examinee hears the ringtone pool-1-thread-10 to start the answer the examinee hears the ringtone pool-1-thread-3 starts the question examinee hears the ringtone pool-1-t Hread-9 start answer the test taker hears the bell pool-1-thread-8 starts the answer the examinee hears the Bell pool-1-thread-6 to start the question Examinee hears the ringtone pool-1-thread-4 starts the question 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 of await method can block me live!

Examination Hall main exam end See Cyclicbarrier's await method can stop me!

See if Cyclicbarrier's await method can clog me up!

See if Cyclicbarrier's await method can clog me up! See if Cyclicbarrier's await method can clog me up! See if Cyclicbarrier's await method can clog me up!

See if Cyclicbarrier's await method can clog me up. See if Cyclicbarrier's await method can clog me up! See if Cyclicbarrier's await method can clog me up!

See if Cyclicbarrier's await method can clog me up!

From the result, Cyclicbarrier's await method does block the continuation of the current thread. Only a count of 0 will be allowed to continue running.


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 exam starts ringing the examinee hears the ringtone pool-1-thread-3 start answer the examinee hears the ringtone pool-1-thread -2 Start answer the test taker hears the bell pool-1-thread-1 starts the answer the examinee pool-1-thread-2 the test taker to hear the Bell pool-1-thread-10 to start the answer the examinee hears the ringtone pool-1-thread-7 starts the answer the examinee hears the ringtone pool-1-thread- 8 Start answer the test taker hears the bell pool-1-thread-9 starts the answer the examinee hears the Bell pool-1-thread-6 starts the question examinee hears the ringtone pool-1-thread-5 to start the question Examinee hears the ringtone pool-1-thread-4 to begin to answer examinee pool-1-thread- 9 hand over to see if Countdownlatch's Countdown method can block me! See how Countdownlatch's Countdown can clog me up! Examinee pool-1-thread-10 hand over to see Countdownlatch of Countdown method can block me live! Examinee pool-1-thread-7 hand over to see Countdownlatch of Countdown method can block me live! Examinee pool-1-thread-4 hand over to see Countdownlatch of Countdown method can block me live. Examinee pool-1-thread-3 hand over to see Countdownlatch of Countdown method can block me live!

Examinee pool-1-thread-5 hand over to see Countdownlatch of Countdown method can block me live! Examinee pool-1-thread-6 hand over to see Countdownlatch of Countdown method can block me live!

Examinee pool-1-thread-1 hand over to see Countdownlatch of Countdown method can block me live! Examinee pool-1-thread-8 hand over to see Countdownlatch of Countdown method can block me live! Examination Hall Main exam End

And Countdownlatch's Countdown method runs after. The thread will continue to run without clogging.



How to use Cyclicbarrier in "Java" Java, illustrative examples

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.