Chan Chen Coding...

          Java CyclicBarrier介紹

          Refer to: http://my.oschina.net/redhouse/blog/60739

          CyclicBarrier 
          (周期障礙)類可以幫助同步,它允許一組線程等待整個(gè)線程組到達(dá)公共屏障點(diǎn)。CyclicBarrier 是使用整型變量構(gòu)造的,其確定組中的線程數(shù)。當(dāng)一個(gè)線程到達(dá)屏障時(shí)(通過調(diào)用 CyclicBarrier.await()),它會(huì)被阻塞,直到所有線程都到達(dá)屏障,然后在該點(diǎn)允許所有線程繼續(xù)執(zhí)行。與CountDownLatch不同的是,CyclicBarrier 所有公共線程都到達(dá)后,可以繼續(xù)執(zhí)行下一個(gè)目標(biāo)點(diǎn),而CountDownLatch第一次到達(dá)指定點(diǎn)后,也就是記數(shù)器減制零,就無法再次執(zhí)行下一目標(biāo)工作。下面主要演義CyclicBarrier 的用法:

          package com.test;

          import java.text.SimpleDateFormat;

          import java.util.Date;

          import java.util.concurrent.BrokenBarrierException;

          import java.util.concurrent.CyclicBarrier;

          import java.util.concurrent.ExecutorService;

          import java.util.concurrent.Executors;

          public class TestCyclicBarrier {

          private static int[] timeWalk = { 5, 8, 15, 15, 10 };  // 徒步需要的時(shí)間

          private static int[] timeSelf = { 1, 3, 4, 4, 5 };           // 自駕游

          private static int[] timeBus = { 2, 4, 6, 6, 7 };           // 旅游大巴

          static String now() {

                      SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

                      return sdf.format(new Date()) + ": ";

          }

          static class Tour implements Runnable {

                      private   int[]     times;

                      private   CyclicBarrier    barrier;

                      private   String   tourName;

          public Tour(CyclicBarrier barrier, String tourName, int[] times) {

                      this.times = times;

                      this.tourName = tourName;

                      this.barrier = barrier;

          }

          public void run() {

                  try {

                           Thread.sleep(times[0] * 1000);

                            System.out.println(now() + tourName + " 合肥");

                            barrier.await();

                            Thread.sleep(times[1] * 1000);

                            System.out.println(now() + tourName + " 南京");

                             barrier.await();

                            Thread.sleep(times[4] * 1000);

                             System.out.println(now() + tourName + " 上海");

                             barrier.await();

                             System.out.println(tourName + "飛機(jī) 合肥");

                   } catch (InterruptedException e) {

                             e.printStackTrace();

                    } catch (BrokenBarrierException e) {

                              e.printStackTrace();

                    }

              }

          }

           

          public static void main(String[] args) {

                        CyclicBarrier barrier = new CyclicBarrier(3);

                         ExecutorService exec = Executors.newFixedThreadPool(3);

                         exec.submit(new Tour(barrier, "徒步", timeWalk));

                         exec.submit(new Tour(barrier, "自駕", timeSelf));

                         exec.submit(new Tour(barrier, "大巴", timeBus));

                        exec.shutdown();

            }

          }

          最后執(zhí)行結(jié)果如下:

          16:24:35: 自駕 合肥

          16:24:36: 大巴 合肥

          16:24:39: 徒步 合肥


          16:24:42: 自駕 南京

          16:24:43: 大巴 南京

          16:24:47: 徒步 南京


          16:24:52: 自駕 上海

          16:24:54: 大巴 上海

          16:24:57: 徒步 上海


          徒步飛機(jī) 合肥

          自駕飛機(jī) 合肥

          大巴飛機(jī) 合肥



          -----------------------------------------------------
          Silence, the way to avoid many problems;
          Smile, the way to solve many problems;

          posted on 2013-05-22 09:57 Chan Chen 閱讀(370) 評(píng)論(0)  編輯  收藏 所屬分類: Scala / Java

          主站蜘蛛池模板: 浮山县| 社旗县| 托里县| 博兴县| 胶州市| 巴彦淖尔市| 桑植县| 尼玛县| 广灵县| 太湖县| 汶川县| 蒙山县| 米易县| 剑阁县| 札达县| 石泉县| 玉树县| 同仁县| 封开县| 洪泽县| 泉州市| 大洼县| 大关县| 黄浦区| 嘉义市| 莱芜市| 平安县| 舞阳县| 高阳县| 漳平市| 南充市| 元朗区| 巴林右旗| 青川县| 新竹县| 满洲里市| 兖州市| 玉林市| 梧州市| 安仁县| 公安县|