Common tools in Java thread concurrent Cyclicbarrier

Source: Internet
Author: User

First, Cyclicbarrier

Cyclicbarrier is a synchronous 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 similar to Countdownlatch is also a counter, the difference is that the number of Cyclicbarrier is called cyclicbarrier.await () to enter the number of waiting threads, When the number of threads reaches the number specified in the initial cyclicbarrier, all threads entering the waiting state are awakened and continue. Cyclicbarrier, as its name means, can be seen as an obstacle, and all threads must be aligned to pass through this barrier. Cyclicbarrier can also take a runnable parameter at the beginning, and this runnable task is executed after the number of Cyclicbarrier is reached and all other threads are awakened.

A, the constructor creates a new Cyclicbarrierthat will start when a given number of participants (threads) are waiting, but it does not perform predefined actions on each barrier.

   Public Cyclicbarrier (intthisnull);}

Demo

1  PackageCom.jalja.org.thread;2 ImportJava.util.concurrent.CyclicBarrier;3 ImportJava.util.concurrent.ExecutorService;4 Importjava.util.concurrent.Executors;5  Public classLocktest {6      Public Static voidmain (String [] args) {7Cyclicbarrier Cyclicbarrier =NewCyclicbarrier (3);8Executorservice executorservice= Executors.newfixedthreadpool (3);9Runnable threadtest=NULL;Ten          for(inti=0;i<3;i++){ Onethreadtest=Newthreadtest (cyclicbarrier); A Executorservice.execute (threadtest); -         } - Executorservice.shutdown (); the     } - } - classThreadTestImplementsrunnable{ -     Privatecyclicbarrier Cyclicbarrier; +      Publicthreadtest (Cyclicbarrier cyclicbarrier) { -          This. cyclicbarrier=Cyclicbarrier; +     } A      Public voidrun () { at         Try { -Thread.Sleep ((Long) (Math.random () *10000)); -System.out.println (Thread.CurrentThread (). GetName () + "Start Execution"); - cyclicbarrier.await (); -}Catch(Exception e) { - e.printstacktrace (); in         } -System.out.println (Thread.CurrentThread (). GetName () + "Execution End"); to     } +}
View Code

B, the constructor creates a new cyclicbarrier that starts when a given number of participants (threads) are waiting and performs a given barrier operation when the barrier is started, which is executed by the last thread that enters barrier

 Public Cyclicbarrier (int  Parties, Runnable barrieraction) {        ifthrowNew illegalargumentexception ();         this. Parties = parties;         this. Count = parties;         this. Barriercommand = barrieraction;    }

Demo: If a Runnable object is passed in when the Cyclicbarrier object is constructed, each time it arrives at a public barrier, the runnable is executed first, and then the waiting runnable is executed. If you change the above example to the following:

1  PackageCom.jalja.org.thread;2 ImportJava.util.concurrent.CyclicBarrier;3 ImportJava.util.concurrent.ExecutorService;4 Importjava.util.concurrent.Executors;5  Public classLocktest {6      Public Static voidmain (String [] args) {7Cyclicbarrier Cyclicbarrier =NewCyclicbarrier (3,NewRunnable () {8              Public voidrun () {9System.out.println (Thread.CurrentThread (). GetName () + "======");Ten                 Try { OneThread.Sleep ((Long) (Math.random () *10000)); A}Catch(Exception e) { - e.printstacktrace (); -                 } theSystem.out.println (Thread.CurrentThread (). GetName () + "======>"); -             } -         }); -Executorservice executorservice= Executors.newfixedthreadpool (3); +Runnable threadtest=NULL; -          for(inti=0;i<3;i++){ +threadtest=Newthreadtest (cyclicbarrier); A Executorservice.execute (threadtest); at         } - Executorservice.shutdown (); -     } - } - classThreadTestImplementsrunnable{ -     Privatecyclicbarrier Cyclicbarrier; in      Publicthreadtest (Cyclicbarrier cyclicbarrier) { -          This. cyclicbarrier=Cyclicbarrier; to     } +      Public voidrun () { -         Try { theThread.Sleep ((Long) (Math.random () *10000)); *System.out.println (Thread.CurrentThread (). GetName () + "Start Execution"); $ cyclicbarrier.await ();Panax Notoginseng}Catch(Exception e) { - e.printstacktrace (); the         } +System.out.println (Thread.CurrentThread (). GetName () + "Execution End"); A     } the}
View Code

Results:

pool-1-thread-2starts execution pool -1-thread-3starts execution pool -1-thread-1 starts execution pool-1-thread-1======  Pool-1-thread-1======>pool-1-thread-1 execution Endpool -1-thread-2 execution End Pool-1- Thread-3 Execution End

Common tools in Java thread concurrent Cyclicbarrier

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.