<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)航:
     
    主站蜘蛛池模板: 曰批全过程免费视频在线观看无码 | 亚洲国产美国国产综合一区二区 | 国产精品免费久久久久电影网| 久久午夜免费视频| 亚洲欧洲日产国码www| 麻花传媒剧在线mv免费观看| 亚洲AV福利天堂一区二区三| 美女内射无套日韩免费播放| 麻豆一区二区三区蜜桃免费| 亚洲伊人tv综合网色| 久操视频免费观看| 毛片大全免费观看| 亚洲欧美日韩综合俺去了| 91免费福利视频| 亚洲AV成人一区二区三区AV| 99爱在线精品视频免费观看9| 亚洲最大的视频网站| 毛片基地免费视频a| 免费无码午夜福利片| 一本色道久久综合亚洲精品高清| 中文字幕在线观看免费| 水蜜桃亚洲一二三四在线| 中文字幕免费高清视频| 亚洲色www永久网站| 四虎永久在线精品免费影视| 全部在线播放免费毛片| 亚洲人成人无码网www国产| 中文字幕无码一区二区免费| 亚洲精品成人网站在线播放| 高清国语自产拍免费视频国产| 色偷偷尼玛图亚洲综合| 亚洲伊人久久大香线蕉综合图片 | 免费电影在线观看网站| 黄色三级三级三级免费看| 国产成人无码综合亚洲日韩| 在线精品一卡乱码免费| 免费人妻精品一区二区三区| 国产AV无码专区亚洲AVJULIA| 无码国产精品一区二区免费| 男女超爽视频免费播放| 日本最新免费网站|