生产者消费者模式引子

最近在看Java多线程这块的知识,遇到不少问题,感觉这个难点比较棘手,下面给出自己写的一个消费者生成者Demo。在写的时候,遇到不少奇怪的现象,经过自己反复琢磨与调试,基本上运行正常,附上代码,供所需者参考。

package com.abc.example;

public class Test {
public static void main(String[] args)
{
<span style="white-space:pre">	</span>Container container=new Container();
<span style="white-space:pre">	</span>Producer pd=new Producer(container);
<span style="white-space:pre">	</span>Consumer cs=new Consumer(container);
<span style="white-space:pre">	</span>new Thread(pd).start();
<span style="white-space:pre">	</span>new Thread(cs).start();
}
}



package com.abc.example;

public class Consumer implements Runnable{
<span style="white-space:pre">	</span>private Container container=null;
<span style="white-space:pre">	</span>int cnt=0;
<span style="white-space:pre">	</span>public Consumer(Container container)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>this.container=container;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>public void run() {
<span style="white-space:pre">	</span>// TODO Auto-generated method stub
<span style="white-space:pre">	</span>while(true)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>synchronized (container) {
<span style="white-space:pre">		</span>try { 
<span style="white-space:pre">			</span>while((cnt=container.getNum())==0)
<span style="white-space:pre">			</span>{ 
<span style="white-space:pre">				</span>System.out.println("空了,请等待");
<span style="white-space:pre">				</span>container.wait(); 
<span style="white-space:pre">			</span>} 
<span style="white-space:pre">			</span>cnt--;
<span style="white-space:pre">			</span>container.setNum(cnt);
<span style="white-space:pre">			</span>System.out.println("拿走一件东西,目前还有"+container.getNum()+"件东西");
<span style="white-space:pre">			</span>Thread.sleep((long)Math.random()*3000);
<span style="white-space:pre">			</span>container.notifyAll();
<span style="white-space:pre">		</span>}catch (InterruptedException e) {
<span style="white-space:pre">		</span>// TODO Auto-generated catch block
<span style="white-space:pre">		</span>e.printStackTrace();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>} 
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>}
}



package com.abc.example;


public class Producer implements Runnable{
private Container container=null;
int cnt=0;
public Producer(Container container)
{
this.container=container;
}
public void run() {
// TODO Auto-generated method stub
while(true)
{
synchronized (container) {
try {
while((cnt=container.getNum())==12)
{
System.out.println("满了,请等待");
container.wait();
} 
cnt++;
container.setNum(cnt);
System.out.println("放入一件东西,目前有"+container.getNum()+"件东西");
Thread.sleep((long)Math.random()*3000);
container.notifyAll();
}catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}



package com.abc.example;


public class Container {
private int num;


public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值