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

          多線程設計方法

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

          一:繼承Thread,重載run方法
          package com.Gavin.threaddesign;
          /**
           * **********************************************
           * @description 繼承Thread 類的多線程程序 設計方法 
           * 
           * 
          @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();
              }

          }


          二:實現Runnable接口,重寫run方法
          package com.Gavin.threaddesign;
          /**
           * **********************************************
           * 多線程對象實現Runnable 接口并且在該類中定義用于啟動線程的run 方法。
           * 這種定義方式的好處在于多線程應用對象可以繼承其它對象而不是必須繼承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:
          今天挑了一段尚學堂的一段線程的視頻,感覺這個例子更容易理解:
          實現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方法出現執(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方法出現執(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í)行結果將是main線程與Thread1線程隨機交替
          主站蜘蛛池模板: 卢氏县| 革吉县| 利津县| 安义县| 东源县| 仁化县| 左贡县| 井冈山市| 陕西省| 曲阜市| 元江| 温州市| 乐安县| 西平县| 汶川县| 射洪县| 元朗区| 孟连| 南部县| 平邑县| 太和县| 齐河县| 西丰县| 巴林右旗| 万州区| 诸暨市| 连山| 荆州市| 山东| 新野县| 高邑县| 南溪县| 隆德县| 巩留县| 安义县| 阳谷县| 江永县| 彝良县| 天水市| 张家口市| 霞浦县|