乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      JDK1.5新特性--java.util.concurrent CyclicBarri...

       goldbomb 2008-01-13

      實際應用中,有時候需要多個線程同時工作以完成同一件事情,而且在完成過程中,往往會等待其他線程都完成某一階段后再執(zhí)行,等所有線程都到達某一個階段后再統(tǒng)一執(zhí)行。

      比如有幾個旅行團需要途經(jīng)深圳、廣州、韶關、長沙最后到達武漢。旅行團中有自駕游的,有徒步的,有乘坐旅游大巴的;這些旅行團同時出發(fā),并且每到一個目的地,都要等待其他旅行團到達此地后再同時出發(fā),直到都到達終點站武漢。

      這時候CyclicBarrier就可以派上用場。CyclicBarrier最重要的屬性就是參與者個數(shù),另外最要方法是await()。當所有線程都調用了await()后,就表示這些線程都可以繼續(xù)執(zhí)行,否則就會等待。

      package concurrent;
      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 {
        // 徒步需要的時間: Shenzhen, Guangzhou, Shaoguan, Changsha, Wuhan
        private static int[] timeWalk = 58151510 };
        // 自駕游
        private static int[] timeSelf = 1344};
        // 旅游大巴
        private static int[] timeBus = 2466};
        
        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[01000);
              System.out.println(now() + tourName + " Reached Shenzhen");
              barrier.await();
              Thread.sleep(times[11000);
              System.out.println(now() + tourName + " Reached Guangzhou");
              barrier.await();
              Thread.sleep(times[21000);
              System.out.println(now() + tourName + " Reached Shaoguan");
              barrier.await();
              Thread.sleep(times[31000);
              System.out.println(now() + tourName + " Reached Changsha");
              barrier.await();
              Thread.sleep(times[41000);
              System.out.println(now() + tourName + " Reached Wuhan");
              barrier.await();
            catch (InterruptedException e) {
            catch (BrokenBarrierException e) {
            }
          }
        }

        public static void main(String[] args) {
          // 三個旅行團
          CyclicBarrier barrier = new CyclicBarrier(3);
          ExecutorService exec = Executors.newFixedThreadPool(3);
          exec.submit(new Tour(barrier, "WalkTour", timeWalk));
          exec.submit(new Tour(barrier, "SelfTour", timeSelf));
          exec.submit(new Tour(barrier, "BusTour", timeBus));
          exec.shutdown();
        }
      }
      運行結果:
      00:02:25: SelfTour Reached Shenzhen
      00:02:25: BusTour Reached Shenzhen
      00:02:27: WalkTour Reached Shenzhen
      00:02:30: SelfTour Reached Guangzhou
      00:02:31: BusTour Reached Guangzhou
      00:02:35: WalkTour Reached Guangzhou
      00:02:39: SelfTour Reached Shaoguan
      00:02:41: BusTour Reached Shaoguan

        本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多