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.