Circular fence: Cyclicbarrier (Commander requires task) reading notes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.