亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Thread actor=new Thread(new Actor(),"Mr.Thread");編譯器不報錯,Mr.thread并沒有傳進去

Thread actor=new Thread(new Actor(),"Mr.Thread");編譯器不報錯,Mr.thread并沒有傳進去

慕神1905738 2016-10-25 19:49:39
將getName()換成Thread.currentThread().getName()就對了,不知道為什么呢?而且貌似進程的執行順序有變化。。。。求指點package?com.imooc.concurrent; public?class?Actor?extends?Thread?{ public?void?run()?{ System.out.println(getName()?+?"is?an?actor"); int?count?=?0; boolean?a=true; while?(a){ System.out.println(getName()?+?"show?begins"?+?(++count)); if?(count==100) { a=false; } if(count%10==0){ try?{ Thread.sleep(1000); }?catch?(InterruptedException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); } } } //System.out.println(getName()?+?"show?begins"?+?(++count)); System.out.println(getName()?+?"show?is?over"); } public?static?void?main(String[]?args)?{ //Thread?actor?=?new?Actor(); // //actor.setName("Mr.Thread"); Thread?actor=new?Thread(new?Actor(),"Mr.Thread"); actor.start(); Thread?actressThread=?new?Thread(new?Actress(),"Ms.Runnable"); actressThread.start(); } } ?class?Actress?implements?Runnable{ public?void?run()?{ System.out.println(Thread.currentThread().getName()?+?"is?an?actor"); int?count?=?0; boolean?a=true; while?(a){ System.out.println(Thread.currentThread().getName()?+?"show?begins"?+?(++count)); if?(count==100) { a=false; } if(count%10==0){ try?{ Thread.sleep(1000); }?catch?(InterruptedException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); } } } System.out.println(Thread.currentThread().getName()?+?"show?is?over"); } }
查看完整描述

3 回答

已采納
?
大咪

TA貢獻785條經驗 獲得超332個贊

public?static?void?main(String[]?args)?{
//Thread?actor?=?new?Actor();
//
//actor.setName("Mr.Thread");
Thread?actor=new?Thread(new?Actor(),"Mr.Thread");????//這里你為什么要改啊。。。?
actor.start();
Thread?actressThread=?new?Thread(new?Actress(),"Ms.Runnable");
actressThread.start();
}

http://img1.sycdn.imooc.com//580f496e0001536020081090.jpg

查看完整回答
反對 回復 2016-10-25
  • 慕神1905738
    慕神1905738
    請問為什么改成Thread actor = new Thread(new Actor(),"Mr.Thread");后 用Thread.currentThread().getName();能得到想要的結果呢?
  • 大咪
    大咪
    因為線程的那個方法是存在的,意思是獲取當前運行的線程名稱
  • 慕神1905738
    慕神1905738
    那也就是說這么寫也是對的咯
點擊展開后面1
  • 3 回答
  • 0 關注
  • 1888 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號