Java multithreading-javasicbarrier

Source: Internet
Author: User
Import Java. util. random; import Java. util. concurrent. cyclicbarrier;/*** cyclicbarrier is similar to countdownlatch and is also a counter. * The difference is that cyclicbarrier calls cyclicbarrier. the number of threads that await () enters the waiting state. * when the number of threads reaches the number specified in the initial state of javasicbarrier, all threads that enter the waiting state are awakened and continue. * Javasicbarrier, like its name, can be seen as an obstacle. * All threads must be completed before they can pass through the obstacle together. * Cyclicbarrier can also contain a runnable parameter at the beginning. * This runnable task is executed before the number of javasicbarrier tasks reaches and all other threads are awakened. */Public class cyclicbarriertest {public static class componentthread implements runnable {cyclicbarrier barrier; // counter int ID; // component ID int [] array; // data array // constructor public componentthread (cyclicbarrier barrier, int [] array, int ID) {This. barrier = barrier; this. id = ID; this. array = array;} public void run () {try {array [ID] = new random (). nextint (100); system. out. println ("component" + ID + "generates:" + array [ID]); // wait for the barrier system here. out. println ("component" + ID + "sleep... "); barrier. await (); system. out. println ("component" + ID + "awaked... "); // calculate the current value in the data array and the subsequent value int result = array [ID] + array [ID + 1]; system. out. println ("component" + ID + "Result:" + result);} catch (exception ex) {}}/ *** test icicbarrier usage */public static void testcyclicbarrier () {final int [] array = new int [3]; javasicbarrier barrier = new javasicbarrier (2, new runnable () {// execute public void run () {system. out. println ("testpolicicbarrier run... "); array [2] = array [0] + array [1] ;}}); // start thread new thread (New componentthread (barrier, array, 0 )). start (); New thread (New componentthread (barrier, array, 1 )). start ();} public static void main (string [] ARGs) {policicbarriertest. testcyclicbarrier ();}}

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.