Java concurrency Programming-levels

Source: Internet
Author: User

Cyclicbarrier is called circular fence, its main method is one: await (). The await () method is not called once, the count is reduced by 1, and the current thread is blocked. When the count is reduced to 0 o'clock, the blocking is lifted, and all the blocked threads on this cyclicbarrier begin to run. After that, if the await () method is called again, the Count becomes N-1 and the new round starts again, which is what Cyclic means.

The use of cyclicbarrier is not difficult, but it requires the exception that is primarily relevant to it. In addition to the common exceptions, the Cyclicbarrier.await () method throws a unique brokenbarrierexception. This exception occurs when a thread waits for the cyclicbarrier to be interrupted or timed out or reset, and the other threads waiting on the Cyclicbarrier are brokenbarrierexception. It means, comrades, do not wait, there is a small partner has been hung, if we continue to wait may have been waiting, all back home.

The Cyclicbarrier.await () method has a return value that indicates that the current thread is the first thread to reach this Barrier.

As with Countdownlatch, Cyclicbarrier can also set a total value in the constructor. Unlike Countdownlatch, the Cyclicbarrier constructor can also accept a Runnable, which is executed when the Cyclicbarrier is freed.

[Java]public class Cyclicbarriertest {public static void main (string[] args) throws IOException, interruptedexception {// If the parameter is changed to 4, but only 3 players are added below, this will wait forever//waits until all parties has invoked await on the this barrier. Cyclicbarrier barrier = new Cyclicbarrier (3); Executorservice executor = Executors.newfixedthreadpool (3); Executor.submit (new Thread (New Runner (barrier, "1th player")); Executor.submit (new Runner (barrier, "number 2nd")), Executor.submit (new Thread (Runner, "3rd")); Executor.shutdown ();}} Class Runner implements Runnable {///A synchronization helper class that allows a set of threads to wait for each other until a public barrier (common barrier point) is reached private cyclicbarrier barrier ;p rivate String name;public Runner (cyclicbarrier barrier, String name) {super (); this.barrier = Barrier;this.name = name;} @Overridepublic void Run () {try {thread.sleep (New Random ()). Nextint (8)); SYSTEM.OUT.PRINTLN (name + "Ready ...");//The await method of barrier will wait until all participants have already raised the await method in this barrier. Barrier.await ();} catch (Interruptedexception e) {e.printstacktrace ();} CAtch (brokenbarrierexception e) {e.printstacktrace ();} SYSTEM.OUT.PRINTLN (name + "Starting!") ");}} [/java]

Countdownlatch applies To work collaboration between a set of threads and another main thread. A main thread waits for the task of a group of workers to complete before continuing its execution is the primary scenario for using Countdownlatch; Cyclicbarrier is used for a group or groups of threads, such as a set of threads that need to agree at a point in time, such as starting a job at the same moment. In addition, the cyclic properties of Cyclicbarrier and the Runnable parameters accepted by the constructors are countdownlatch.

Java concurrency Programming-levels

Related Article

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.