Chan Chen Coding...

          Java CyclicBarrier介紹

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

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

          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 + "飛機 合肥");

                   } 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: 徒步 上海


          徒步飛機 合肥

          自駕飛機 合肥

          大巴飛機 合肥



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

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

          主站蜘蛛池模板: 广河县| 嘉善县| 罗甸县| 南华县| 汝州市| 龙山县| 余干县| 临湘市| 柘城县| 株洲市| 鄢陵县| 溧水县| 天峻县| 滨海县| 嘉义县| 莲花县| 久治县| 旅游| 漳浦县| 凭祥市| 清河县| 宜兴市| 望城县| 榆林市| 紫云| 南靖县| 衡山县| 陆河县| 宜宾市| 名山县| 苍溪县| 西林县| 南部县| 武平县| 巨鹿县| 长顺县| 郓城县| 岳普湖县| 和硕县| 郑州市| 宁蒗|