编写一个生产与消费的程序,具体要求:店主告诉生产者生产一组产品,生产者生产出之后运给店主,店主通知生产者我们这里只放得下一组产品你等等再生产,店主通知消费者取走这组产品,店主通知消费者你等等,我们这里没有产品了,店主告诉生产者生产一组产品,生产者生产出之后运给店主,店主通知生产者... ...就是这样一个运作流程(如下图所示).

代码:
- classShengChanimplementsRunnable
- {
- DianZhud;
-
publicShengChan(DianZhud)
- {
-
this.d=d;
- }
-
publicvoidrun()
- {
-
inti=0;
-
while(true)
- {
-
System.out.println("");//很奇怪,去掉这行就不能交替运行了,知道的网友,请赐教,谢谢.
-
if(i==0)
- {
-
d.put("001","洗衣机");
- }
-
else
- {
-
d.put("002","冰箱");
- }
-
i=(i+1)%2;
- }
- }
- }
-
classXiaoFeiimplementsRunnable
- {
- DianZhud;
-
publicXiaoFei(DianZhud)
- {
-
this.d=d;
- }
-
publicvoidrun()
- {
-
while(true)
- {
- d.get();
- }
- }
- }
-
classDianZhu
- {
-
privateStringproID="";
-
privateStringproName="";
-
privatebooleanisFull=false;//店主放的产品是否满了?默认为false;表示未满.
-
publicsynchronizedvoidput(StringproID,StringproName)
- {
-
if(isFull==false)//如果是空的.
- {
-
//生产产品
-
this.proID=proID;
-
this.proName=proName;
-
isFull=true;//产品已经生产好
-
//System.out.println(Thread.currentThread().getName()+":生产完毕!");
-
notify();//通知[消费线程]工作,注意:notify();具备有先启动,等该线程休息,才生效的特性
- }
-
else//如果是满的,[生产线程]休息
- {
-
//System.out.println(Thread.currentThread().getName()+":休息!\n");
-
try{wait();}catch(Exceptione){e.printStackTrace();}
- }
- }
-
publicsynchronizedvoidget()
- {
-
if(isFull==true)//如果有产品
- {
-
//消费者取走产品
-
System.out.println("proID:"+proID);
-
System.out.println("proName:"+proName);
-
isFull=false;//消费者已经取走产品
-
//System.out.println(Thread.currentThread().getName()+":取产品完毕!");
-
notify();//通知[生产线程]工作
- }
-
else//如果没有产品,[消费线程]休息
- {
-
//System.out.println(Thread.currentThread().getName()+":休息!\n");
-
try{wait();}catch(Exceptione){e.printStackTrace();}
- }
- }
- }
-
publicclassShengChanLian
- {
-
publicstaticvoidmain(String[]args)
- {
-
DianZhud=newDianZhu();
-
ShengChans=newShengChan(d);//创建对象的同时把DianZhu对象传过去
-
XiaoFeix=newXiaoFei(d);
-
//启动线程
-
newThread(s).start();
-
newThread(x).start();
- }
- }
class ShengChan implements Runnable
{
DianZhu d;
public ShengChan(DianZhu d)
{
this.d=d;
}
public void run()
{
int i=0;
while(true)
{
System.out.println("");//很奇怪,去掉这行就不能交替运行了,知道的网友,请赐教,谢谢.
if(i==0)
{
d.put("001","洗衣机");
}
else
{
d.put("002","冰箱");
}
i=(i+1)%2;
}
}
}
class XiaoFei implements Runnable
{
DianZhu d;
public XiaoFei(DianZhu d)
{
this.d=d;
}
public void run()
{
while(true)
{
d.get();
}
}
}
class DianZhu
{
private String proID="";
private String proName="";
private boolean isFull=false;//店主放的产品是否满了?默认为false;表示未满.
public synchronized void put(String proID,String proName)
{
if(isFull==false)//如果是空的.
{
//生产产品
this.proID=proID;
this.proName=proName;
isFull=true;//产品已经生产好
// System.out.println(Thread.currentThread().getName()+":生产完毕!");
notify();//通知[消费线程]工作,注意:notify();具备有先启动,等该线程休息,才生效的特性
}
else//如果是满的,[生产线程]休息
{
// System.out.println(Thread.currentThread().getName()+":休息!\n");
try{wait();}catch(Exception e){e.printStackTrace();}
}
}
public synchronized void get()
{
if(isFull==true)//如果有产品
{
//消费者取走产品
System.out.println("proID:"+proID);
System.out.println("proName:"+proName);
isFull=false;//消费者已经取走产品
// System.out.println(Thread.currentThread().getName()+":取产品完毕!");
notify();//通知[生产线程]工作
}
else//如果没有产品,[消费线程]休息
{
// System.out.println(Thread.currentThread().getName()+":休息!\n");
try{wait();}catch(Exception e){e.printStackTrace();}
}
}
}
public class ShengChanLian
{
public static void main(String[]args)
{
DianZhu d=new DianZhu();
ShengChan s=new ShengChan(d);//创建对象的同时把DianZhu对象传过去
XiaoFei x=new XiaoFei(d);
//启动线程
new Thread(s).start();
new Thread(x).start();
}
}输出结果:

<style type="text/css">
<!--
.gh_bg
{font-size:12px;
width:100%;
background-color:#E7E5DC}
.csdn_des
{background-color:#E7E5DC;
height:26px;
line-height:26px;
overflow:hidden;
float:left;
padding:4px}
.csdn_call
{background-color:#000;
color:#FFF;
height:26px;
line-height:26px;
float:left;
border-right:solid 3 #F00;
padding:4px}
.string
{color:#F00;
font-weight:bold}
.gh_clear
{clear:both;
overflow:hidden;
width:0px;
height:0px}
-->
</style>
相关文章:
-
2021-07-06
-
2022-03-04
-
2021-05-25
-
2022-01-16
-
2022-12-23
-
2021-05-24
-
2021-06-30
-
2021-09-02
猜你喜欢
-
2021-08-11
-
2022-12-23
-
2021-07-08
-
2021-08-03
-
2022-12-23
-
2021-08-12
-
2022-12-23
相关资源
-
下载
2023-02-07
-
下载
2021-06-06
-
下载
2023-03-13
-
下载
2021-06-24