快捷导航

Thread actor=new Thread(new Actor(),"Mr.Thread");编译器不报错,Mr.

将getName()换成Thread.currentThread().getName()就对了,不知道为什么呢?而且貌似进程的执行顺序有变化。。。。求指点packagecom.imooc.concurrent;publicclassActorextendsThread{publicvoidrun(){System.out.println(getName()+"isanactor");intcount=0;booleana=true;while(a){System.out.println(getName()+"showbegins"+(++count));if(count==100){a=false;}if(count%10==0){try{Thread.sleep(1000);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}//System.out.println(getName()+"showbegins"+(++count));System.out.println(getName()+"showisover");}publicstaticvoidmain(String[]args){//Threadactor=newActor();////actor.setName("Mr.Thread");Threadactor=newThread(newActor(),"Mr.Thread");actor.start();ThreadactressThread=newThread(newActress(),"Ms.Runnable");actressThread.start();}}classActressimplementsRunnable{publicvoidrun(){System.out.println(Thread.currentThread().getName()+"isanactor");intcount=0;booleana=true;while(a){System.out.println(Thread.currentThread().getName()+"showbegins"+(++count));if(count==100){a=false;}if(count%10==0){try{Thread.sleep(1000);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}System.out.println(Thread.currentThread().getName()+"showisover");}}

免责声明:本内容仅代表回答者见解不代表本站观点,请谨慎对待。

版权声明:作者保留权利,不代表本站立场。

回复

使用道具 举报

参与会员1

publicstaticvoidmain(String[]args){//Threadactor=newActor();////actor.setName("Mr.Thread");Threadactor=newThread(newActor(),"Mr.Thread");//这里你为什么要改啊。。。?actor.start();ThreadactressThread=newThread(newActress(),"Ms.Runnable");actressThread.start();}
回复

使用道具 举报

可能感兴趣的问答

发新帖
  • 微信访问
  • 手机APP