Multi-threaded waiting with Java concurrency tool fence (cyclicbarrier) to perform common tasks at the same time

Source: Internet
Author: User

Cyclicbarrier, the name is a circular fence, the fence is obvious is to stop a bunch of "things", where "things" is a thread, when the number of threads specified by the fence is not reached, all the threads wait, to reach the specified number, together to perform the following tasks. API documentation explains:

A synchronization aid that allows a sets of threads to all of the wait for each and the reach a common barrier point.  Cyclicbarriers is useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called cyclic because it can being re-used after the waiting threads is released.

Set a scene below, in the sports field, all athletes free to go to the starting line, before the start of the starter can be free to warm up, but once all the players have reached the starting line and ready, the referee orders. The procedure is as follows:

  

 PackageCom.smikevon.concurrent.tools;ImportJava.util.Random;Importjava.util.concurrent.BrokenBarrierException;ImportJava.util.concurrent.CyclicBarrier;/*** @description: Requirements: 10 athletes to carry out a hundred-meter race, before the race to carry out their own warm-up exercise, and then unified start, the referee began to remember. * @date: September 17, 2014 PM 4:22:15*/ Public classTestcyclicbarrier { Public Static voidMain (string[] args)throwsinterruptedexception {string[] children= {"Xiao Ming (No. 1th)", "Xiao Qiang (2nd)", "Little Sea (3rd)", "Little Dragon (No. 4th)", "Xiao Wu (5th)", "Xiao Yong (6th)", "Peng (7th)", "Xiao Fei (8th)", "Xiao Feng (9th)", "Xiao Tao (10th)"}; FinalRandom random =NewRandom (System.currenttimemillis ()); FinalCyclicbarrier barrier =NewCyclicbarrier (10,NewRunnable () { Public voidrun () {Thread.CurrentThread (). SetName (Referee); Doing ("Stand, prepare, run.");        }        }); //let 10 athletes move.         for(String child:children) {NewThread (NewRunnable () { Public voidrun () {Try{doing ("I'm going to start doing my warm-up exercise!" "); intTime = Random.nextint (20000);                        Thread.Sleep (time); Doing ("I have done the warm-up exercise!" Total time Elapsed "+long.parselong (time+" ")/1000+" Seconds! "); //wait for other threads to executebarrier.await (); Doing ("I'm ready, the current time is:" +System.currenttimemillis ()); } Catch(interruptedexception e) {e.printstacktrace (); } Catch(brokenbarrierexception e) {e.printstacktrace ();        }}},child). Start (); }    }    /*** @Description: Print the name of the outgoing thread *@parammsg * @returType: void*/     Public Static voiddoing (String msg) {System.out.println (Thread.CurrentThread (). GetName ()+":"+msg); }}

Multi-threaded waiting with Java concurrency tool fence (cyclicbarrier) to perform common tasks at the same time

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.