將getName()換成Thread.currentThread().getName()就對(duì)了,不知道為什么呢?而且貌似進(jìn)程的執(zhí)行順序有變化。。。。求指點(diǎn)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貢獻(xiàn)785條經(jīng)驗(yàn) 獲得超332個(gè)贊
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(); }
點(diǎn)擊展開后面1條
添加回答
舉報(bào)
0/150
提交
取消