Cyclicbarrier is used to simulate all threads arriving at a critical condition after the next step, Cyclicbarrier used in the Run function
Here is an example of use
Package Com.condition;import Java.util.concurrent.brokenbarrierexception;import Java.util.concurrent.cyclicbarrier;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;public class Cyclicbarriertest {public static void main (string[] args) {Executorservice ThreadPool = Executors.newcachedthreadpool (); final Cyclicbarrier cb = new Cyclicbarrier (3); for (int i = 0; i < 3;i++) {Ru Nnable runnable = new runnable () {@Overridepublic void run () {try {Thread.Sleep ((long) (Math.random () *10000))} catch (Int Erruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} System.out.println (1+thread.currentthread (). GetName () + "currently available" +cb.getnumberwaiting ()); try {cb.await ();} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (Brokenbarrierexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} try {thread.sleep ((long) (Math.random () *10000)),} catch (Interruptedexception e) {//TODO auto-generated catch BloCke.printstacktrace ();} System.out.println (2+thread.currentthread (). GetName () + "currently available" +cb.getnumberwaiting ()); try {cb.await ();} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (Brokenbarrierexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}; Threadpool.execute (runnable);}}}
Learn multi-threaded 6---fence