<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)航:
     
    主站蜘蛛池模板: 亚洲国产三级在线观看| 精品久久久久成人码免费动漫| 亚洲精品无码久久久久牙蜜区| 亚洲日韩精品国产3区| 国产成人无码免费看视频软件| 在线观看亚洲免费| 久久精品国产精品亚洲| 亚洲综合激情另类小说区| 亚洲国产区男人本色| 精品国产污污免费网站| 午夜时刻免费入口| 亚洲精品国产精品乱码不99| 国产免费AV片在线观看| 亚洲精品无码久久毛片波多野吉衣| 1000部禁片黄的免费看| 国产亚洲美女精品久久久2020| 亚洲精品无码永久在线观看男男| 成人免费在线观看网站| 亚洲欧洲精品国产区| 永久免费的网站在线观看| 无码天堂亚洲国产AV| 永久免费在线观看视频| 国产成人精品亚洲2020| 成人A片产无码免费视频在线观看| 免费无码又爽又高潮视频| 亚洲经典在线中文字幕| 成人五级毛片免费播放| 全黄A免费一级毛片| 毛片免费在线观看网站| 黄页网址在线免费观看| 国产美女a做受大片免费| 亚洲粉嫩美白在线| 成人毛片免费观看视频| 一级毛片免费观看不收费| 免费无码AV片在线观看软件| 精品亚洲国产成AV人片传媒| 中文字幕免费人成乱码中国| www国产亚洲精品久久久日本| 亚洲熟妇久久精品| 四虎成人免费影院网址| 国产精品黄页免费高清在线观看|