Can be understood as a circular fence, a fence is an obstacle. If we set the counter to 10, then after the first 10 threads, the counter will be zeroed, then the next batch of 10 threads, which is the meaning of the loop fence. Constructor:
Public Cyclicbarrier (int Parties, Runnable barrieraction)
Parties: Total count, which is the total number of threads involved. Barrieraction when the counter completes the count, the following code shows the commander requires 10 soldiers to complete the task, first gather 10 and then go together to complete the task, and so the commander will announce the completion of the mission!
Public classCyclicbarrierdemo { Public Static classSoldierImplementsRunnable {PrivateString soldier; Private FinalCyclicbarrier cyclic; PublicSoldier (cyclicbarrier cyclic, String Soldier) { This. Soldier =soldier; This. Cyclic =cyclic; } /*** When an object implementing interface <code>Runnable</code> was used * to create a Threa D, starting the thread causes the object ' s * <code>run</code> method to being called in that separately E xecuting * thread. * <p> * The general contract of the method <code>run</code> are that it could * take any AC tion whatsoever. * * @seeThread#run ()*/@Override Public voidrun () {Try { //wait for all the soldiers to arrivecyclic.await (); DoWork (); //waiting for all the soldiers to finish their workcyclic.await (); } Catch(Interruptedexception e) {//The thread is interrupted during the wait processE.printstacktrace (); } Catch(Brokenbarrierexception e) {//indicates that the current cyclicbarrier is corrupted. The system cannot wait until all threads are aligned.E.printstacktrace (); } } voiddoWork () {Try{thread.sleep (Math.Abs (NewRandom (). Nextint ()% 10000)); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println (Soldier+ ": Task Complete"); } } Public Static classBarrierrunImplementsRunnable {BooleanFlag; intN; PublicBarrierrun (BooleanFlagintN) { This. Flag =Flag; This. N =N; } /*** When an object implementing interface <code>Runnable</code> was used * to create a Threa D, starting the thread causes the object ' s * <code>run</code> method to being called in that separately E xecuting * thread. * <p> * The general contract of the method <code>run</code> are that it could * take any AC tion whatsoever. * * @seeThread#run ()*/@Override Public voidrun () {if(flag) {System.out.println ("Commander: [Soldier] + N +", Mission accomplished!] "); } Else{System.out.println ("Commander: [Soldier" + N + "one, assemble complete!]"); Flag=true; } } } Public Static voidMain (string[] args) {Final intN = 10; Thread[] Allsoldier=NewThread[n]; BooleanFlag =false; Cyclicbarrier cyclic=NewCyclicbarrier (N,Newbarrierrun (flag, N)); //set the barrier point, primarily to perform this methodSYSTEM.OUT.PRINTLN ("Rally Team!")); for(inti = 0; i < N; i++) {System.out.println ("Soldier" + i + "Report!"); Allsoldier[i]=NewThread (NewSoldier (cyclic, "soldier" +i)); Allsoldier[i].start (); } }}
Results:
Circular fence: Cyclicbarrier (Commander requires task) reading notes