第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

為什么運(yùn)行結(jié)果與老師不同?

代碼與老師一樣,但運(yùn)行結(jié)果如下:

https://img1.sycdn.imooc.com//5adb2cd50001b86d02280248.jpg

并不是每個(gè)線程分別演出10次,這是什么原因呢?看了下別的答案好像和電腦的CPU核數(shù)有關(guān),但還不是很清楚,有人能比較清楚地介紹下嗎?

另外,怎么控制才能和老師的輸出結(jié)果一樣呢?

萬(wàn)分感謝!


以下是我的代碼

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é)束了");
????}
}


正在回答

2 回答

嘿嘿,多運(yùn)行幾次你就會(huì)發(fā)現(xiàn),結(jié)果不會(huì)總是一樣的,多核計(jì)算機(jī)的時(shí)間片分配問(wèn)題

0 回復(fù) 有任何疑惑可以回復(fù)我~
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é)束了");

??? }

}


0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

神億般的存在

你沒(méi)說(shuō)那里導(dǎo)致這個(gè)問(wèn)題啊,代碼和我打的一樣,還是出現(xiàn)這個(gè)問(wèn)題
2018-05-21 回復(fù) 有任何疑惑可以回復(fù)我~
#2

慕斯卡3197864 提問(wèn)者 回復(fù) 神億般的存在

看別的答案,好像是因?yàn)椋憾嗪薈PU+運(yùn)行結(jié)果的動(dòng)態(tài)性
2018-05-24 回復(fù) 有任何疑惑可以回復(fù)我~
#3

RSF 回復(fù) 慕斯卡3197864 提問(wèn)者

那你知道怎么改成跟老師一樣的嗎??/
2018-06-20 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

為什么運(yùn)行結(jié)果與老師不同?

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)