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

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

加了同步代碼關(guān)鍵字,但是還是會(huì)出現(xiàn)線程安全問題,怎么回事???

public?class?VolatileT?{
	private?int?number?=?0;
	
	public?int?getNumber(){
		return?this.number;
	}
	
	public?void?increase(){
		synchronized(this){
		this.number++;
		}
	}
	
	public?static?void?main(String[]?arg){
		final?VolatileT?vo?=?new?VolatileT();
		for(int?i=0;i<500;i++){
			new?Thread(new?Runnable(){
				@Override
				public?void?run()?{
					vo.increase();
				}
			}).start();
			//System.out.println(vo.getNumber());
			System.out.println(Thread.activeCount());
		}
		if(Thread.activeCount()>1){
			Thread.yield();
		}
		System.out.println("result="+?vo.getNumber());
	}
}


正在回答

2 回答

number 沒有加Volatile 關(guān)鍵字

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

是淚還是累

你這回答還被采納,我的天。synchronized已經(jīng)確保了可見性了
2022-08-10 回復(fù) 有任何疑惑可以回復(fù)我~

number加了Volatile 關(guān)鍵字他也會(huì)出錯(cuò)的,因?yàn)闆]有保證原子性,才出現(xiàn)這種問題

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

樓上說錯(cuò)了把

if(Thread.activeCount()>1){

????????????Thread.yield();

????????}

這個(gè)錯(cuò)了吧 你寫if的話不管滿不滿足都會(huì)往下執(zhí)行,所以輸出的時(shí)候線程還沒有跑完,當(dāng)然會(huì)錯(cuò)咯

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

qq_往煙_0

對(duì),if改為while
2017-12-03 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

加了同步代碼關(guān)鍵字,但是還是會(huì)出現(xiàn)線程安全問題,怎么回事???

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

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

幫助反饋 APP下載

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

公眾號(hào)

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