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         //創(chuàng)建線程thread1
           14         ThreadUserExtends thread1=new ThreadUserExtends();
           15         //創(chuàng)建thread2時(shí)以實(shí)現(xiàn)了Runnable接口的ThreadUseRunnable類實(shí)例為參數(shù)
           16         Thread thread2 = new Thread(new ThreadUserRunable(),"SecondThread");
           17          
           18         /**
           19          * thread1.setPriority(6);//設(shè)置thread1的優(yōu)先級(jí)為6
           20          * 優(yōu)先級(jí)將決定CPU空出時(shí),處于就緒狀態(tài)的線程誰(shuí)先占領(lǐng)CPU開(kāi)始運(yùn)行
           21          * 優(yōu)先級(jí)范圍1到10,MIN_PRIORITY,MAX_PRIORITY,NORM_PAIORITY
           22          * 新線程繼承創(chuàng)建她的父線程優(yōu)先級(jí),父線程通常有普通優(yōu)先級(jí)即5NORM_PRIORITY
           23          */
           24         thread1.start();//啟動(dòng)線程thread1使之處于就緒狀態(tài)
           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休眠過(guò)長(zhǎng),主線程殺掉了thread1!-----------");
           38          }else{
           39             System.out.println("-----------主線程沒(méi)發(fā)現(xiàn)thread1,thread1已醒順序執(zhí)行結(jié)束了!-----------");
           40          }
           41          
           42          thread2.start();//啟動(dòng)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休眠過(guò)長(zhǎng),主線程殺掉了thread2!-----------");
           54          }else{
           55              System.out.println("-----------主線程沒(méi)發(fā)現(xiàn)thread2,thread2已醒順序執(zhí)行結(jié)束了!-----------");
           56          }
           57          
           58          System.out.println("-----------程序結(jié)束按任意鍵繼續(xù)!-----------");
           59          try{
           60              System.in.read();
           61          }catch (IOException e){
           62              System.out.println(e.toString());
           63          }
           64     }
           65 }
           66 
           67 /**
           68  * 通過(guò)繼承Thread類,并實(shí)現(xiàn)它的抽象方法run()
           69  * 適當(dāng)時(shí)候創(chuàng)建這一Thread子類的實(shí)例來(lái)實(shí)現(xiàn)多線程機(jī)制
           70  * 一個(gè)線程啟動(dòng)后(也就是進(jìn)入就緒狀態(tài))一旦獲得CPU將自動(dòng)調(diào)用它的run()方法
           71  */
           72 class ThreadUserExtends extends Thread{
           73     
           74     ThreadUserExtends(){ 
           75         //無(wú)參的構(gòu)造函數(shù)
           76     }
           77     
           78     public void run(){
           79         System.out.println("+++++++++++我是Thread子類的線程實(shí)例!+++++++++++++");
           80         System.out.println("+++++++++++我將掛起10秒!+++++++++++++");
           81         System.out.println("+++++++++++回到主線程,請(qǐng)稍等,剛才主線程掛起可能還沒(méi)醒過(guò)來(lái)!+++++++++++++");
           82         /**
           83          * 如果該run()方法順序執(zhí)行完了,線程將自動(dòng)結(jié)束,而不會(huì)被主線程殺掉
           84          * 但如果休眠時(shí)間過(guò)長(zhǎng),則線程還存活,可能被stop()殺掉
           85          */
           86         try{
           87             sleep(10000);//掛起10秒
           88         }catch(InterruptedException e){
           89             e.printStackTrace();
           90             return;
           91         }
           92     }    
           93 }
           94 
           95 /**
           96  *通過(guò)實(shí)現(xiàn)Runnable接口中的run()方法,再以這個(gè)實(shí)現(xiàn)了run()方法的類
           97  *為參數(shù)創(chuàng)建Thread的線程實(shí)例
           98  *以這個(gè)實(shí)現(xiàn)了Runnable接口中run()方法的類為參數(shù)創(chuàng)建Thread類的線程實(shí)例
           99  */
          100 class ThreadUserRunable implements Runnable{
          101     
          102     ThreadUserRunable(){//構(gòu)造函數(shù)
          103         
          104     }
          105     
          106     public void run(){
          107         System.out.println("***********我是Thread類的線程實(shí)例并以實(shí)現(xiàn)了Runnable接口的類為參數(shù)!***********");
          108         System.out.println("***********我將掛起10秒!***********");
          109         System.out.println("***********回到主線程,請(qǐng)稍等,剛才主線程掛起可能還沒(méi)醒過(guò)來(lái)!***********");
          110         /**
          111          * 如果該run()方法順序執(zhí)行完了,線程將自動(dòng)結(jié)束,而不會(huì)被主線程殺掉
          112          * 但如果休眠時(shí)間過(guò)長(zhǎng),則線程還存活,可能被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) 評(píng)論(0)  編輯  收藏 所屬分類: java技術(shù)分區(qū)

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

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 巴马| 武清区| 车险| 元江| 江安县| 龙口市| 得荣县| 渭源县| 水城县| 伽师县| 阿拉善左旗| 称多县| 石城县| 光山县| 平江县| 丰顺县| 遂平县| 五河县| 建湖县| 寿阳县| 桐梓县| 镇康县| 长子县| 天津市| 伽师县| 恩平市| 开平市| 梁平县| 龙州县| 双牌县| 乌兰察布市| 绩溪县| 乌拉特中旗| 承德县| 通山县| 宜川县| 崇礼县| 北宁市| 莱西市| 离岛区| 和平区|