Examples of fences

Source: Internet
Author: User
Tags static class
Package cn.hc.sort.thread;
Import java.util.concurrent.BrokenBarrierException;

Import Java.util.concurrent.CyclicBarrier;
 /** * Fence Instance * Created by YHC on 2015/5/6. * * Public class Cyclicbarriertest {private static class implements runnable{private final Cyclicbar
        RieR barrier;

        Private final String Provincename;
            Private (Cyclicbarrier barrier, String provincename) {this.barrier = barrier;
        This.provincename = Provincename;
            @Override public void Run () {System.out.println ("Start statistics" +provincename+ "data, generate statistics ...");
                Try {//todo Complex statistical computation operations Thread.Sleep (2000);
                SYSTEM.OUT.PRINTLN (provincename+ "Statistical data, Generation completed.");
                Barrier.await (); System.out.println ("Calculating" +provincename+ "data in national data");}
            catch (Interruptedexception e) {e.printstacktrace (); catch (BrokenbarrierexcePtion e) {e.printstacktrace ();
        }} public static void Main (string[] args) {cyclicbarrier barrier=new cyclicbarrier (3);
        String [] provincearray=new string[]{"Hubei province", "Hunan province", "Jiangxi Province"}; for (int i = 0; i < provincearray.length i++) {thread thread=new thread (new (barrier,provincearray[
            I]));
        Thread.Start ();
 }
    }


}

Start to statistics Hubei Province data, generate statistical data ....
Start to statistics Hunan Province data, generate statistics ....
Start Statistics Jiangxi Province data, generate statistical data ....
Jiangxi Province statistic data, the generation completes.
Hunan Province statistic data, produce complete.
Hubei province statistic data, produce complete.
Calculates the example of Hubei province data in the national data.
Calculates the example of Jiangxi Province data in the national data.
Calculates the example of Hunan province data in the national data.


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.