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 彭偉 閱讀(218) 評論(0)  編輯  收藏 所屬分類: java技術分區

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

          導航

          統計

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 黄大仙区| 建始县| 柳州市| 桐梓县| 新乡县| 玉山县| 正宁县| 靖江市| 安仁县| 惠水县| 新河县| 云和县| 五华县| 铅山县| 平果县| 南华县| 延边| 武功县| 庄浪县| 宜川县| 米脂县| 惠水县| 宜兴市| 长子县| 黎平县| 郧西县| 鸡泽县| 南漳县| 库尔勒市| 德江县| 西盟| 洛宁县| 广南县| 奉贤区| 太湖县| 郴州市| 来宾市| 定陶县| 玛沁县| 洛隆县| 吉隆县|