posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          多線程設(shè)計方法

          Posted on 2009-07-16 22:32 Gavin.lee 閱讀(373) 評論(0)  編輯  收藏 所屬分類: 多線程

          一:繼承Thread,重載run方法
          package com.Gavin.threaddesign;
          /**
           * **********************************************
           * @description 繼承Thread 類的多線程程序 設(shè)計方法 
           * 
           * 
          @author Gavin.lee
           * @date Jul 16, 2009    2:25:41 PM
           * 
          @version 1.0
           ***********************************************
           
          */

          class Consumer extends Thread {
              
          int nTime;
              String strConsumer;

              
          public Consumer(int nTime, String strConsumer) {
                  
          this.nTime = nTime;
                  
          this.strConsumer = strConsumer;
              }


              
          public void run() {
                  
          while (true{
                      
          try {
                          System.out.println(
          "Consumer name:" + strConsumer + "\n");
                          Thread.sleep(nTime);
                      }
           catch (Exception e) {
                          e.printStackTrace();
                      }

                  }

              }


              
          static public void main(String args[]) {
                  Consumer aConsumer 
          = new Consumer(1000"aConsumer");
                  aConsumer.start();
                  Consumer bConsumer 
          = new Consumer(2000"bConsumer");
                  bConsumer.start();
                  Consumer cConsumer 
          = new Consumer(3000"cConsumer ");
                  cConsumer.start();
              }

          }


          二:實現(xiàn)Runnable接口,重寫run方法
          package com.Gavin.threaddesign;
          /**
           * **********************************************
           * 多線程對象實現(xiàn)Runnable 接口并且在該類中定義用于啟動線程的run 方法。
           * 這種定義方式的好處在于多線程應(yīng)用對象可以繼承其它對象而不是必須繼承Thread 類,
           * 從而能夠增加類定義的邏輯性
           * 
          @author Gavin.lee
           * @date Jul 16, 2009    2:27:28 PM
           * 
          @version 1.0
           ***********************************************
           
          */

          public class Consumer2 implements Runnable {

              
          int nTime;
              String strConsumer;

              
          public Consumer2(int nTime, String strConsumer) {
                  
          this.nTime = nTime;
                  
          this.strConsumer = strConsumer;
              }


              
          public void run() {

                  
          while (true{
                      
          try {
                          System.out.println(
          "Consumer name:" + strConsumer + "\n");
                          Thread.sleep(nTime);
                      }
           catch (Exception e) {
                          e.printStackTrace();
                      }

                  }

              }


              
          static public void main(String args[]) {
                  Thread aConsumer 
          = new Thread(new Consumer(1000"aConsumer"));
                  aConsumer.start();

              }

          }



          2009年7月19日20:23:14 PS:
          今天挑了一段尚學堂的一段線程的視頻,感覺這個例子更容易理解:
          實現(xiàn)Runnable接口
          package com.Gavin.threaddesign;

          public class TestThread {
              
          public static void main(String args[]) {
                  Thread1 t1 
          = new Thread1();
                  Thread t 
          = new Thread(t1);
                  t.start();    
          //main方法出現(xiàn)執(zhí)行分支
                  
                  
          for(int i = 0; i < 50; i++{
                      System.err.println(
          "main thread:" + i);
                  }

              }
                  
          }

          class Thread1 implements Runnable{
              
          public void run() {
                  
          for(int i = 0; i < 50; i++{
                      System.err.println(
          "Thread1 thread:" + i);
                  }

              }
              
          }

          繼承Thread類
          package com.Gavin.threaddesign;

          public class TestThread {
              
          public static void main(String args[]) {
                  Thread1 t1 
          = new Thread1();
                  t1.start();    
          //main方法出現(xiàn)執(zhí)行分支
                  
                  
          for(int i = 0; i < 50; i++{
                      System.err.println(
          "main thread:" + i);
                  }

              }
                  
          }

          class Thread1 extends Thread{
              
          public void run() {
                  
          for(int i = 0; i < 50; i++{
                      System.err.println(
          "Thread1 thread:" + i);
                  }

              }
              
          }

          執(zhí)行結(jié)果將是main線程與Thread1線程隨機交替
          主站蜘蛛池模板: 右玉县| 苏尼特右旗| 察雅县| 肥东县| 德惠市| 肇源县| 北票市| 西畴县| 吕梁市| 登封市| 屯留县| 河曲县| 永康市| 沐川县| 东方市| 十堰市| 山西省| 杂多县| 阿克| 宣城市| 二连浩特市| 绵竹市| 衢州市| 和田县| 随州市| 长沙县| 山东| 自治县| 淄博市| 裕民县| 双桥区| 垣曲县| 竹溪县| 错那县| 务川| 屯留县| 兴和县| 浮山县| 峡江县| 遂溪县| 仁寿县|