Cyh的博客

          Email:kissyan4916@163.com
          posts - 26, comments - 19, trackbacks - 0, articles - 220

          線程5--線程join

          Posted on 2009-06-02 20:37 啥都寫點 閱讀(170) 評論(0)  編輯  收藏 所屬分類: J2SE
          關鍵技術:
          • 調用線程A的join方法表示當前線程必須等待線程A運行完后才能夠繼續運行。
          • 可以為join提供參數,指定當前線程的最長等待時間(毫秒數)。

          package book.thread;

          /**
           * 線程的結合。
           * 當一個線程需要等待另一個線程結束時,叫做線程的結合。
           
          */
          public class JoinThread {
              
          /**    自定義線程類 */
              
          static class ThreadA extends Thread{
                  
          //線程的ID
                  private int ID = 0;
                  
          //線程運行時循環的次數
                  private int whileTimes = 0;
                  
          public ThreadA(int id, int times){
                      
          this.ID = id;
                      
          this.whileTimes = times;
                  }
                  
          public void run(){
                      System.out.println(
          "ThreadA" + this.ID + " begin!");
                      
          int i=0
                      
          try {
                          
          //連續循環whileTimes次
                          while (i < this.whileTimes){
                              System.out.println(
          "ThreadA-" + this.ID + "" + i++);
                              
          //sleep方法將當前線程休眠。
                              Thread.sleep(200);
                          }
                      } 
          catch (InterruptedException e) {
                      }

                      System.out.println(
          "ThreadA" + this.ID + " end!");
                  }
              }
              
          public static void main(String[] args) {
                  
          //新建4個線程對象
                  Thread thread1 = new ThreadA(13);
                  Thread thread2 
          = new ThreadA(22);
                  Thread thread3 
          = new ThreadA(32);
                  Thread thread4 
          = new ThreadA(44);
                  
          //啟動所有線程
                  System.out.println("Main method begin. To start 4 threads!");
                  thread1.start();
                  thread2.start();
                  thread3.start();
                  thread4.start();
                  
          //等待所有線程運行結束
                  try {
                      thread1.join();
                      thread2.join();
                      thread3.join();
                      thread4.join();
                  } 
          catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  
          //此時所有線程都運行結束
                  System.out.println("Main method end! All 4 threads are ended");
              }
          }



                                                                                                                 --    學海無涯
                  

          主站蜘蛛池模板: 奈曼旗| 徐水县| 通道| 甘洛县| 曲松县| 金门县| 巨鹿县| 峡江县| 东丽区| 龙游县| 毕节市| 类乌齐县| 板桥市| 油尖旺区| 荥阳市| 蕉岭县| 双流县| 越西县| 石首市| 嘉峪关市| 天峻县| 宕昌县| 聂拉木县| 和顺县| 麟游县| 铜梁县| 璧山县| 咸宁市| 平和县| 西宁市| 堆龙德庆县| 长治市| 宁明县| 福建省| 美姑县| 吐鲁番市| 嘉定区| 惠安县| 丽江市| 化隆| 克拉玛依市|