Cyclicbarrier literally means a barrier that can be recycled, and the thing to do is to get a thread to be blocked when it reaches a barrier, until the last thread arrives at barrier, the barrier is released, and all the threads that are blocked by the barrier will continue to run.
The default constructor for Cyclicbarrier is cyclicbarrier (int parties), which represents the number of threads that the barrier intercepts, and each thread calls the await method to tell Cyclicbarrier I have reached the barrier, The current thread is then blocked. The sample code is as follows:
Public classCyclicbarriertest {Private StaticCyclicbarrier barrier =NewCyclicbarrier (2); Public Static voidMain (string[] args)throwsbrokenbarrierexception, interruptedexception {NewThread (NewRunnable () {@Override Public voidrun () {Try{barrier.await (); } Catch(interruptedexception e) {e.printstacktrace (); } Catch(brokenbarrierexception e) {e.printstacktrace (); } System.out.print ("Thread1"); }}). Start (); Barrier.await (); System.out.println ("Mainthread"); }}
Because the scheduling of the child and the main thread is determined by the CPU, each thread is likely to take precedence, resulting in two different results, with the following possible results:
Mainthread
Thread1
If you change the number of Cyclicbarrier barrier from 2 to 3, the main thread and the child thread will wait forever because no third thread is executing the await method, that is, no third thread has reached the barrier, so two threads that have previously reached the barrier will not continue to execute.
Cyclicbarrier also provides a high-level constructor, cyclicbarrier (int parties, Runnable barrieraction), which is used to prioritize when the thread arrives at the barrier Barrieraction The logic inside the thread. For example, in the following scenario, after all the threads have been processed, print "All done" and refer to the following code:
Public classCyclicBarrierTest2 {Private StaticCyclicbarrier barrier =NewCyclicbarrier (2,NewDonethread ()); Public Static voidMain (string[] args)throwsbrokenbarrierexception, interruptedexception {NewThread (NewRunnable () {@Override Public voidrun () {System.out.println ("Thread1"); Try{barrier.await (); } Catch(interruptedexception e) {e.printstacktrace (); } Catch(brokenbarrierexception e) {e.printstacktrace (); }}). Start (); System.out.println ("Mainthread"); Barrier.await (); } Static classDonethreadImplementsrunnable{@Override Public voidrun () {System.out.println ("All Done"); } }}
The output results are as follows:
Mainthreadthread1all Done
Therefore, Cyclicbarrier is often used for processing, multi-threaded calculation of the final consolidation of the results of the scene.
The difference between Cyclicbarrier and Countdownlatch:
The 1.CountDownLatch counter can only be used once, while the cyclicbarrier counter may be reset using the Reset method, so cyclicbarrier can handle the scenario where the thread calculates the error when the calculation is repeated.
2.CyclicBarrier also provides other useful methods, such as getnumberwaiting can get the number of threads waiting, the IsBroken method can understand whether the blocked thread is interrupted, and so on.
Java Concurrency Tool class synchronization barrier Cyclicbarrier