<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    中文JAVA技術平等自由協作創造

    Java專題文章博客和開源

    常用鏈接

    統計

    最新評論

    Java實現主線程等待子線程

      本文介紹兩種主線程等待子線程的實現方式,以5個子線程來說明:
      1、使用Thread的join()方法,join()方法會阻塞主線程繼續向下執行。
      2、使用java.util.concurrent中的CountDownLatch,是一個倒數計數器。初始化時先設置一個倒數計數初始值,每調用一次countDown()方法,倒數值減一,他的await()方法會阻塞當前進程,直到倒數至0.
      join方式代碼如下:
      package com.test.thread;
      import java.util.ArrayList;
      import java.util.List;
      public class MyThread extends Thread
      {
      public MyThread(String name)
      {
      this.setName(name);
      }
      @Override
      public void run()
      {
      System.out.println(this.getName() + " staring…");
      System.out.println(this.getName() + " end…");
      }
      /**
      * @param args
      */
      public static void main(String[] args)
      {
      System.out.println("main thread starting…");
      List<MYTHREAD> list = new ArrayList<MYTHREAD>();
      for (int i = 1; i <= 5; i++)
      {
      MyThread my = new MyThread("Thrad " + i);
      my.start();
      list.add(my);
      }
      try
      {
      for (MyThread my : list)
      {
      my.join();
      }
      }
      catch (InterruptedException e)
      {
      e.printStackTrace();
      }
      System.out.println("main thread end…");
      }
      }
      運行結果如下:
      main thread starting…
      Thrad 2 staring…
      Thrad 2 end…
      Thrad 4 staring…
      Thrad 4 end…
      Thrad 1 staring…
      Thrad 1 end…
      Thrad 3 staring…
      Thrad 3 end…
      Thrad 5 staring…
      Thrad 5 end…
      main thread end…
      CountDownLatch方式代碼如下:
      package com.test.thread;
      import java.util.concurrent.CountDownLatch;
      public class MyThread2 extends Thread
      {
      private CountDownLatch count;
      public MyThread2(CountDownLatch count, String name)
      {
      this.count = count;
      this.setName(name);
      }
      @Override
      public void run()
      {
      System.out.println(this.getName() + " staring…");
      System.out.println(this.getName() + " end…");
      this.count.countDown();
      }
      /**
      * @param args
      */
      public static void main(String[] args)
      {
      System.out.println("main thread starting…");
      CountDownLatch count = new CountDownLatch(5);
      for (int i = 1; i <= 5; i++)
      {
      MyThread2 my = new MyThread2(count, "Thread " + i);
      my.start();
      }
      try
      {
      count.await();
      }
      catch (InterruptedException e)
      {
      e.printStackTrace();
      }
      System.out.println("main thread end…");
      }
      }
      運行結果如下:
      main thread starting…
      Thread 2 staring…
      Thread 2 end…
      Thread 4 staring…
      Thread 4 end…
      Thread 1 staring…
      Thread 1 end…
      Thread 3 staring…
      Thread 3 end…
      Thread 5 staring…
      Thread 5 end…
      main thread end…托福答案

    posted on 2014-08-25 22:00 好不容易 閱讀(190) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    PK10開獎 PK10開獎
    主站蜘蛛池模板: 国产日本亚洲一区二区三区| 亚洲午夜国产精品无码| 亚洲国产av一区二区三区丶| 95免费观看体验区视频| 亚洲成在人线av| 国产成人免费视频| 精品无码一区二区三区亚洲桃色| 久草免费福利视频| 久久精品国产亚洲av麻| 最近中文字幕高清免费中文字幕mv| 亚洲综合区图片小说区| 动漫黄网站免费永久在线观看| 学生妹亚洲一区二区| 热久久精品免费视频| 黄页网站在线观看免费| 亚洲精品久久久www| baoyu122.永久免费视频| 亚洲狠狠久久综合一区77777| 亚洲一区二区免费视频| 亚洲国产欧美国产综合一区| 国产一级淫片视频免费看| 好猛好深好爽好硬免费视频| 好看的电影网站亚洲一区| ww在线观视频免费观看| 亚洲变态另类一区二区三区 | 亚洲AV无码乱码国产麻豆穿越 | 可以免费看的卡一卡二| 亚洲欧美国产国产综合一区| 国产一级特黄高清免费大片| 成人性生交大片免费看中文| 亚洲性一级理论片在线观看| 日本特黄特色aa大片免费| 成年免费a级毛片免费看无码| 亚洲网红精品大秀在线观看| 成年女人午夜毛片免费视频| jzzjzz免费观看大片免费| 亚洲欧洲日产国码二区首页| 拔擦拔擦8x华人免费久久| 青青操免费在线观看| 亚洲码和欧洲码一码二码三码| 亚洲美女高清一区二区三区|