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

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

大神幫我看看自己寫的多線程哪里出了問題

主要功能是兩個(gè)線程操作一個(gè)變量,一個(gè)讓它+1,一個(gè)讓它-1然后交替輸出

public?class?Res?{
	private?boolean?flag?=?true;
	private?int?x?=?1;

	public?synchronized?void?print1()?{
		if?(!flag)?{
			try?{
				this.wait();
			}?catch?(InterruptedException?e)?{
				e.printStackTrace();
			}
		}
		System.out.println(x++);
		flag?=?false;
		this.notify();

	}

	public?synchronized?void?print2()?{

		if?(flag)?{
		}
		try?{
			this.wait();
		}?catch?(InterruptedException?e)?{
			e.printStackTrace();
		}

		System.out.println(x--);
		flag?=?true;
		this.notify();
	}

}
public?class?PrintOne?implements?Runnable{
	private?Res?r;
	public?PrintOne(Res?r){
		this.r=r;
	}
public?void?run(){
	while(true){
		r.print1();
	}
}
}
public?class?PrintTwo?implements?Runnable{
	private?Res?r;
	public?PrintTwo(Res?r){
		this.r=r;
	}
public?void?run(){
	while(true){
		r.print2();
	}
}
}
public?class?Test?{
	public?static?void?main(String[]?args)?{
		Res?r=new?Res();
		new?Thread(new?PrintOne(r)).start();
		new?Thread(new?PrintTwo(r)).start();
	}

}

運(yùn)行結(jié)果就是輸出一個(gè)1,就停住了,哪里死鎖了還是全部等待了

正在回答

1 回答

你的print2方法里,wait方法沒有在if分支里。

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

Juneava 提問者

哦哦,老是犯低級錯(cuò)誤!感謝指導(dǎo)!看的真仔細(xì)!
2016-11-20 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

大神幫我看看自己寫的多線程哪里出了問題

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

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

幫助反饋 APP下載

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

公眾號

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