Cyclicbarrier is called circular fence, its main method is one: await (). The await () method is not called once, the count is reduced by 1, and the current thread is blocked. When the count is reduced to 0 o'clock, the blocking is lifted, and all the blocked threads on this cyclicbarrier begin to run. After that, if the await () method is called again, the Count becomes N-1 and the new round starts again, which is what Cyclic means.
The use of cyclicbarrier is not difficult, but it requires the exception that is primarily relevant to it. In addition to the common exceptions, the Cyclicbarrier.await () method throws a unique brokenbarrierexception. This exception occurs when a thread waits for the cyclicbarrier to be interrupted or timed out or reset, and the other threads waiting on the Cyclicbarrier are brokenbarrierexception. It means, comrades, do not wait, there is a small partner has been hung, if we continue to wait may have been waiting, all back home.
The Cyclicbarrier.await () method has a return value that indicates that the current thread is the first thread to reach this Barrier.
As with Countdownlatch, Cyclicbarrier can also set a total value in the constructor. Unlike Countdownlatch, the Cyclicbarrier constructor can also accept a Runnable, which is executed when the Cyclicbarrier is freed.
[Java]public class Cyclicbarriertest {public static void main (string[] args) throws IOException, 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 the this barrier. Cyclicbarrier barrier = new Cyclicbarrier (3); Executorservice executor = Executors.newfixedthreadpool (3); Executor.submit (new Thread (New Runner (barrier, "1th player")); Executor.submit (new Runner (barrier, "number 2nd")), Executor.submit (new Thread (Runner, "3rd")); Executor.shutdown ();}} Class Runner implements Runnable {///A synchronization helper class that allows a set of threads to wait for each other until a public barrier (common barrier point) is reached private cyclicbarrier barrier ;p rivate String name;public Runner (cyclicbarrier barrier, String name) {super (); this.barrier = Barrier;this.name = name;} @Overridepublic void Run () {try {thread.sleep (New Random ()). Nextint (8)); SYSTEM.OUT.PRINTLN (name + "Ready ...");//The await method of barrier will wait until all 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!") ");}} [/java]
Countdownlatch applies To work collaboration between a set of threads and another main thread. A main thread waits for the task of a group of workers to complete before continuing its execution is the primary scenario for using Countdownlatch; Cyclicbarrier is used for a group or groups of threads, such as a set of threads that need to agree at a point in time, such as starting a job at the same moment. In addition, the cyclic properties of Cyclicbarrier and the Runnable parameters accepted by the constructors are countdownlatch.
Java concurrency Programming-levels