Cyclicbarrier use of Java

Source: Internet
Author: User

1, class Description:

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.

2, the use of the scene:

When all subtasks are required to complete, the main task is executed, and you can choose to use cyclicbarrier at this time.

3. Common methods: await
    await ()          throws Interruptedexception,                 brokenbarrierexception
The
waits until all participants have already raised the   await method in this barrier. If the current thread is not the last thread that will arrive, it will be disabled for scheduling purposes, and the thread will remain dormant until one of the following conditions occurs:

 

  • The last thread arrives, or
  • another thread interrupts the current thread , or
  • one other thread interrupts another waiting thread, or
  • another thread is waiting for barrier to time out, or
  • some other thread barrier the   Reset () .

If the current thread:

  • has set the interrupt state of the thread when entering this method, or
  • is interrupted while waiting

throws   Interru Ptedexception and clears the interrupted state of the current thread. Barrier is   if the thread is waiting, reset () , or barrier  is corrupted when   is called, await   Or if either thread is waiting, throw   brokenbarrierexception   exception.

If any thread is   interrupted while waiting, all other waiting threads will throw   brokenbarrierexception   exception and put barrier in a corrupt state.

If the current thread is the last thread to be reached and a non-empty barrier operation is provided in the constructor method, the current thread will run the operation until another thread is allowed to continue running. If an exception occurs during the execution of a barrier operation, the exception is propagated to the current thread and the barrier is placed in a corrupt state.

 

Return:
The index of the current thread reached, where Index getParties() 1 indicates the first thread to arrive, 0 indicates the last thread to reach
Thrown:
InterruptedException -If the current thread is interrupted while waiting
BrokenBarrierException -if another thread is interrupted or timed out while the current thread is waiting, or if the barrier is reset, or when the call await is Barri The ER is damaged, or the barrier operation (if present) fails because of an exception.
4. Relevant examples

Running, waiting for everyone to be ready before starting:

 Public classCyclicbarriertest { Public Static voidMain (string[] args)throwsIOException, interruptedexception {//If the parameter is changed to 4, but only 3 players are added below, this will wait forever.//Waits until all parties has invoked await on this barrier. Cyclicbarrier barrier =NewCyclicbarrier (3); Executorservice Executor= Executors.newfixedthreadpool (3); Executor.submit (NewThread (NewRunner (barrier, "contestant number 1th"))); Executor.submit (NewThread (NewRunner (barrier, "contestant number 2nd"))); Executor.submit (NewThread (NewRunner (barrier, "contestant number 3rd")));      Executor.shutdown (); }  }    classRunnerImplementsRunnable {//a synchronization helper class that allows a group of threads to wait until they reach a common barrier point (common barrier points)    Privatecyclicbarrier Barrier; PrivateString name;  PublicRunner (cyclicbarrier barrier, String name) {Super();  This. Barrier =barrier;  This. Name =name; } @Override Public voidrun () {Try{Thread.Sleep (1000 * (NewRandom ()). Nextint (8)); SYSTEM.OUT.PRINTLN (Name+ "Ready ..."); //Barrier's await method waits until all the participants have already raised the await method in this barrier. barrier.await (); } Catch(interruptedexception e) {e.printstacktrace (); } Catch(brokenbarrierexception e) {e.printstacktrace (); } System.out.println (Name+ "Starting!" "); }  }  



Output Result:
Player number 3rd is ready ... Player number 2nd is ready ... Player number 1th is ready ... Contestant number 1th starts! Contestant Number 2nd starts! Contestant Number 3rd starts!
Reprint Address: http://www.itzhai.com/the-introduction-and-use-of-cyclicbarrier.html

Cyclicbarrier use of Java

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.