<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我的Java路上那些事兒

    快樂(lè)成長(zhǎng)
    posts - 110, comments - 101, trackbacks - 0, articles - 7
      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
    class NewThread implements Runnable {
    Thread t;
    public NewThread() {
    t = new Thread(this,"Demo thread");
    System.out.println("Child thread : " + t);
    t.run();
    }
    public void run(){
    try{
    for( int i = 5; i > 0; i --){
    System.out.println("Child thread :" + i);
    Thread.sleep(500);
    }

    }catch(InterruptedException e){
    System.out.println("Child interrupted.");
    }
    System.out.println("Exiting child thread.");

    }
    }

    public class TestDemo{
    public static void main(String args[]){
    new NewThread();
    try{
    for( int i = 5; i > 0; i --){
    System.out.println("Main thread :" + i);
    Thread.sleep(1000);
    }
    }catch(InterruptedException e){
    System.out.println("Main interrupted.");
    }
    System.out.println("Exiting Main thread.");
    }
    }

    這是一個(gè)實(shí)現(xiàn)多線程的程序,運(yùn)行結(jié)果如下:
    Child thread : Thread[Demo thread,5,main]
    Main thread :5
    Child thread :5
    Child thread :4
    Main thread :4
    Child thread :3
    Child thread :2
    Main thread :3
    Child thread :1
    Exiting child thread.
    Main thread :2
    Main thread :1
    Exiting Main thread.

    試想,如果把 start()改成run()會(huì)出現(xiàn)什么結(jié)果?
    修改之后運(yùn)行結(jié)果:
    Child thread : Thread[Demo thread,5,main]
    Child thread :5
    Child thread :4
    Child thread :3
    Child thread :2
    Child thread :1
    Exiting child thread.
    Main thread :5
    Main thread :4
    Main thread :3
    Main thread :2
    Main thread :1
    Exiting Main thread.
    程序運(yùn)行成為了單線程順序執(zhí)行。為什么?
    start方法:用來(lái)啟動(dòng)一個(gè)線程, 這時(shí)此線程是處于就緒狀態(tài), 并沒(méi)有運(yùn)行。 然后通過(guò)此Thread類調(diào)用方法run()來(lái)完成其運(yùn)行操作的, 這里方法run()稱為線程體, 它包含了要執(zhí)行的這個(gè)線程的內(nèi)容, run方法運(yùn)行結(jié)束, 此線程終止, 而CPU再運(yùn)行其它線程,
    直接用run方法: 這只是調(diào)用一個(gè)方法而已, 程序中依然只有主線程--這一個(gè)線程, 其程序執(zhí)行路徑還是只有一條, 這樣就沒(méi)有達(dá)到寫線程的目的。
    記住:線程就是為了更好地利用CPU,提高程序運(yùn)行速率的!

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲电影免费在线观看| 99久久免费国产特黄| 毛片免费视频观看| 亚洲不卡1卡2卡三卡2021麻豆| 无码国产精品一区二区免费vr| 亚洲一区二区三区夜色| 99久久免费中文字幕精品| 久久精品国产亚洲AV嫖农村妇女| 日韩精品久久久久久免费| 亚洲理论片在线中文字幕| 在线看片韩国免费人成视频| 天堂亚洲国产中文在线| 日韩在线a视频免费播放| 特黄特色大片免费| 亚洲中文字幕在线乱码| 香蕉免费一区二区三区| 亚洲13又紧又嫩又水多| 免费日本黄色网址| 成人一区二区免费视频| 99人中文字幕亚洲区| 国语成本人片免费av无码| 午夜在线亚洲男人午在线| 国产综合亚洲专区在线| 久久国产精品免费看| 欧洲 亚洲 国产图片综合| 免费a在线观看播放| 久草免费福利视频| 在线亚洲午夜片AV大片| 亚洲国产精品无码久久九九| 日本免费中文视频| 亚洲成AV人片高潮喷水| 亚洲AV无码一区二区二三区软件 | 中文字幕在亚洲第一在线| 日本一道本不卡免费| 亚洲综合色丁香婷婷六月图片 | 一区二区三区四区免费视频| 亚洲1区1区3区4区产品乱码芒果| 免费人成视网站在线观看不卡| 伊人免费在线观看| 亚洲精品无播放器在线播放| 亚洲色欲久久久综合网东京热|