為什么運(yùn)行結(jié)果與老師不同?
代碼與老師一樣,但運(yùn)行結(jié)果如下:

并不是每個(gè)線程分別演出10次,這是什么原因呢?看了下別的答案好像和電腦的CPU核數(shù)有關(guān),但還不是很清楚,有人能比較清楚地介紹下嗎?
另外,怎么控制才能和老師的輸出結(jié)果一樣呢?
萬分感謝!
以下是我的代碼
public?class?Actor?extends?Thread?{
????public?void?run()?{
???????System.out.println(getName()+"登臺(tái)演出了!");
???????int?count=0;
???????boolean?keepRunning=true;
???????while(keepRunning)?{
???????????System.out.println(getName()+"登臺(tái)演出了:"+(++count));
???????????if(count==100)
??????????????keepRunning=false;
???????????if(count%10==0)?{
??????????????try?{
??????????????????Thread.sleep(1000);
??????????????}?catch?(InterruptedException?e)?{
??????????????????e.printStackTrace();
??????????????}
???????????}
???????}
???????System.out.println(getName()+"的演出結(jié)束了");
????}
???
????public?static?void?main(String[]?args)?{
???????Thread?actor=?new?Actor();?????
???????actor.setName("Mr.?Thread");
???????actor.start();
???????Thread?actress?=?new?Thread(new?Actress(),?"Ms.?Runnable");
???????actress.start();
????}
}
?
class?Actress?implements?Runnable{
????public?void?run()?{
???????System.out.println(Thread.currentThread().getName()+"登臺(tái)演出了!");
???????int?count=0;
???????boolean?keepRunning=true;
???????while(keepRunning)?{
???????????System.out.println(Thread.currentThread().getName()+"登臺(tái)演出了:"+(++count));
???????????if(count==100)
??????????????keepRunning=false;
???????????if(count%10==0)?{
??????????????try?{
??????????????????Thread.sleep(1000);
??????????????}?catch?(InterruptedException?e)?{
??????????????????e.printStackTrace();
??????????????}
???????????}
???????}
???????System.out.println(Thread.currentThread().getName()+"的演出結(jié)束了");
????}
}
2018-07-17
嘿嘿,多運(yùn)行幾次你就會(huì)發(fā)現(xiàn),結(jié)果不會(huì)總是一樣的,多核計(jì)算機(jī)的時(shí)間片分配問題
2018-04-21
??? public void run() {
?????? System.out.println(getName()+"登臺(tái)演出了!");
?????? int count=0;
?????? boolean keepRunning=true;
?????? while(keepRunning) {
?????????? System.out.println(getName()+"登臺(tái)演出了:"+(++count));
?????????? if(count==100)
????????????? keepRunning=false;
?????????? if(count%10==0) {
????????????? try {
????????????????? Thread.sleep(1000);
????????????? } catch (InterruptedException e) {
????????????????? e.printStackTrace();
????????????? }
?????????? }
?????? }
?????? System.out.println(getName()+"的演出結(jié)束了");
??? }
???
??? public static void main(String[] args) {
?????? Thread actor= new Actor();?????
?????? actor.setName("Mr. Thread");
?????? actor.start();
?????? Thread actress = new Thread(new Actress(), "Ms. Runnable");
?????? actress.start();
??? }
}
?
class Actress implements Runnable{
??? public void run() {
?????? System.out.println(Thread.currentThread().getName()+"登臺(tái)演出了!");
?????? int count=0;
?????? boolean keepRunning=true;
?????? while(keepRunning) {
?????????? System.out.println(Thread.currentThread().getName()+"登臺(tái)演出了:"+(++count));
?????????? if(count==100)
????????????? keepRunning=false;
?????????? if(count%10==0) {
????????????? try {
????????????????? Thread.sleep(1000);
????????????? } catch (InterruptedException e) {
????????????????? e.printStackTrace();
????????????? }
?????????? }
?????? }
?????? System.out.println(Thread.currentThread().getName()+"的演出結(jié)束了");
??? }
}