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