問題:?Baozipu類和Chihuo類都創建了Baozi的對象為什么main方法還要傳個Baozi的對象參數進去呢?這里有點不解?難道是因為鎖?
package?cn.XianCheng;
public?class?Baozipu?extends?Thread{
????//創建一個包子類型的變量
????????Baozi?bz?=?new?Baozi();
????//使用帶參構造方法為這個包子賦值
????public?Baozipu(Baozi?cz){
????????this.bz?=?cz?;
????}
????//設置線程任務生產包子
????@Override
????public?void?run()?{
????????????while(true){
????//必須使用同步技術保證只有一個線程在執行??這里用?同步代碼塊
????????synchronized(bz){
????????//判斷有沒有包子
????????????if(bz.flag==true){
?????????????//有包子包子鋪就進入等待狀態
????????????????bz.notify();
????????????????try?{
????????????????????bz.wait();
????????????????}?catch?(InterruptedException?e)?{
????????????????????e.printStackTrace();
????????????????}
????????????}
????????????if(bz.flag==false){
????????????//沒有包子就生產包子
???????????????bz.pi?=?"薄皮";
???????????????bz.xian?=?"牛肉餡";
????????????????System.out.println("正在生產包子"+Thread.currentThread().getName());
????????????????try?{
????????????????????Thread.sleep(1000);
????????????????}?catch?(InterruptedException?e)?{
????????????????????e.printStackTrace();
????????????????}
????????????????bz.flag?=?true;
????????????????System.out.println("包子鋪已經生產好了:"+bz.pi+bz.xian+"包子"+Thread.currentThread().getName());
????????????????bz.notify();
????????????????try?{
????????????????????Thread.sleep(1000);
????????????????}?catch?(InterruptedException?e)?{
????????????????????e.printStackTrace();
????????????????}
????????????}
????????}
???????}
????}
}-------------------------------------------------------------------------------------------------------------package?cn.XianCheng;
public?class?Chihuo?extends?Thread{
????????Baozi?bz?=?new?Baozi();
????????public?Chihuo(Baozi?cz){
????????????this.bz?=?cz?;
????????}
????@Override
????public?void?run()?{
????????while(true){
????????????synchronized?(bz){
????????????????if(bz.flag?==?false){
????????????????????bz.notify();
????????????????????try?{
????????????????????????bz.wait();
????????????????????}?catch?(InterruptedException?e)?{
????????????????????????e.printStackTrace();
????????????????????}
????????????????}
????????????????if(bz.flag?==?true){
????????????????????System.out.println("正在吃:"+bz.pi+bz.xian+"包子"+Thread.currentThread().getName());
????????????????????try?{
????????????????????????Thread.sleep(1000);
????????????????????}?catch?(InterruptedException?e)?{
????????????????????????e.printStackTrace();
????????????????????}
????????????????????bz.flag?=?false?;
????????????????????System.out.println("吃完了"+Thread.currentThread().getName());
????????????????????System.out.println("-------------------------------------");
????????????????????bz.notify();
????????????????????try?{
????????????????????????Thread.sleep(1000);
????????????????????}?catch?(InterruptedException?e)?{
????????????????????????e.printStackTrace();
????????????????????}
????????????????}
????????????}
????????}
????}
}--------------------------------------------------------------------------------------------------package?cn.XianCheng;
public?class?Baozi?{
????//皮
????//餡
????//包子的狀態
?????String?pi;
?????String?xian;
?????boolean?flag?=?false;
}--------------------------------------------------------------------------------------------------package?cn.XianCheng;
public?class?XianChengmain?{
????
????public?static?void?main(String[]?args)?{
????????????Baozi?cz?=?new?Baozi();//創建包子對象
????????????new?Baozipu(cz).start();//開啟包子鋪線程
????????????new?Chihuo(cz).start();//開啟吃貨線程
????}
}
關于多線程的問題 麻煩各位大佬進來看看 指導指導 十分感謝
Dusdii
2019-05-16 11:19:12