JAVA 線程的例子

            1 package test.capture;
            2 
            3 import java.io.IOException;
            4 
            5 public class MultiThread {
            6 
            7     /**
            8      * JAVA線程很好的例子
            9      */
           10     public static void main(String[] args) {
           11         
           12         System.out.println("-----------我是主線程---------!");
           13         //創建線程thread1
           14         ThreadUserExtends thread1=new ThreadUserExtends();
           15         //創建thread2時以實現了Runnable接口的ThreadUseRunnable類實例為參數
           16         Thread thread2 = new Thread(new ThreadUserRunable(),"SecondThread");
           17          
           18         /**
           19          * thread1.setPriority(6);//設置thread1的優先級為6
           20          * 優先級將決定CPU空出時,處于就緒狀態的線程誰先占領CPU開始運行
           21          * 優先級范圍1到10,MIN_PRIORITY,MAX_PRIORITY,NORM_PAIORITY
           22          * 新線程繼承創建她的父線程優先級,父線程通常有普通優先級即5NORM_PRIORITY
           23          */
           24         thread1.start();//啟動線程thread1使之處于就緒狀態
           25         
           26         System.out.println("-----------主線程將掛起7秒!-----------");
           27         try{
           28            Thread.sleep(7000);//主線程掛起7秒
           29         }catch (InterruptedException e){
           30             return;
           31         }
           32         
           33         System.out.println("-----------又回到了主線程!-----------");
           34         
           35          if(thread1.isAlive()){
           36              thread1.stop();//如果thread1還存在則殺掉他
           37              System.out.println("-----------thread1休眠過長,主線程殺掉了thread1!-----------");
           38          }else{
           39             System.out.println("-----------主線程沒發現thread1,thread1已醒順序執行結束了!-----------");
           40          }
           41          
           42          thread2.start();//啟動thread2
           43          System.out.println("-----------主線程又將掛起7秒!-----------");
           44          try{
           45              Thread.sleep(7000);//主線程掛起7秒
           46          }catch (InterruptedException e){
           47              return;
           48          }
           49          
           50          System.out.println("-----------又回到了主線程!-----------");
           51          if(thread2.isAlive()){
           52              thread2.stop();//如果thread2還存在則殺掉他
           53              System.out.println("-----------thread2休眠過長,主線程殺掉了thread2!-----------");
           54          }else{
           55              System.out.println("-----------主線程沒發現thread2,thread2已醒順序執行結束了!-----------");
           56          }
           57          
           58          System.out.println("-----------程序結束按任意鍵繼續!-----------");
           59          try{
           60              System.in.read();
           61          }catch (IOException e){
           62              System.out.println(e.toString());
           63          }
           64     }
           65 }
           66 
           67 /**
           68  * 通過繼承Thread類,并實現它的抽象方法run()
           69  * 適當時候創建這一Thread子類的實例來實現多線程機制
           70  * 一個線程啟動后(也就是進入就緒狀態)一旦獲得CPU將自動調用它的run()方法
           71  */
           72 class ThreadUserExtends extends Thread{
           73     
           74     ThreadUserExtends(){ 
           75         //無參的構造函數
           76     }
           77     
           78     public void run(){
           79         System.out.println("+++++++++++我是Thread子類的線程實例!+++++++++++++");
           80         System.out.println("+++++++++++我將掛起10秒!+++++++++++++");
           81         System.out.println("+++++++++++回到主線程,請稍等,剛才主線程掛起可能還沒醒過來!+++++++++++++");
           82         /**
           83          * 如果該run()方法順序執行完了,線程將自動結束,而不會被主線程殺掉
           84          * 但如果休眠時間過長,則線程還存活,可能被stop()殺掉
           85          */
           86         try{
           87             sleep(10000);//掛起10秒
           88         }catch(InterruptedException e){
           89             e.printStackTrace();
           90             return;
           91         }
           92     }    
           93 }
           94 
           95 /**
           96  *通過實現Runnable接口中的run()方法,再以這個實現了run()方法的類
           97  *為參數創建Thread的線程實例
           98  *以這個實現了Runnable接口中run()方法的類為參數創建Thread類的線程實例
           99  */
          100 class ThreadUserRunable implements Runnable{
          101     
          102     ThreadUserRunable(){//構造函數
          103         
          104     }
          105     
          106     public void run(){
          107         System.out.println("***********我是Thread類的線程實例并以實現了Runnable接口的類為參數!***********");
          108         System.out.println("***********我將掛起10秒!***********");
          109         System.out.println("***********回到主線程,請稍等,剛才主線程掛起可能還沒醒過來!***********");
          110         /**
          111          * 如果該run()方法順序執行完了,線程將自動結束,而不會被主線程殺掉
          112          * 但如果休眠時間過長,則線程還存活,可能被stop()殺掉
          113          */
          114         try{
          115             Thread.sleep(10000);//掛起10秒
          116         }catch(InterruptedException e){
          117             e.printStackTrace();
          118             return;
          119         }
          120     }
          121 }
          122 
          123 
          124 

          posted on 2009-07-17 21:47 彭偉 閱讀(220) 評論(0)  編輯  收藏 所屬分類: java技術分區

          <2009年7月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導航

          統計

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 天长市| 阜新市| 江山市| 普洱| 揭阳市| 巴里| 富顺县| 香格里拉县| 甘谷县| 浪卡子县| 三台县| 承德市| 读书| 朝阳区| 社会| 贡嘎县| 庆云县| 大城县| 博野县| 荣昌县| 莱州市| 裕民县| 湖北省| 磐石市| 澄迈县| 马公市| 湖南省| 恩平市| 额敏县| 孙吴县| 武穴市| 墨玉县| 南通市| 太康县| 五指山市| 三亚市| 南充市| 旬邑县| 马尔康县| 洛隆县| 嘉黎县|