多线程之间的通信生产者和消费者

package day12;
/*多线程之间的通信
 *  生产者生产 商品
 *  
 *  消费者消费商品
 * 
 *  商品有自己的 生产方法和消费方法 
 *  
 * */
public class ThreadDemo5 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//创建商品对象
		Good g=new Good();
		// 创建生产者对象
		PorduterDemo p=new PorduterDemo(g);
		 // 创建消费者对象
		ConsumerDemo c=new ConsumerDemo(g);
	   // 创建生产者线程
		new Thread(p).start();
		new Thread(p).start();
		//  创建消费者线程
		new Thread(c).start();
		new Thread(c).start();
	}
}
/*商品类 
 *   属性  名称 
 *        编号
 *        标记变量
 *    方法    生产的方法
 *          消费的方法
 * */
class Good 
{
	private String name;
	private int count=1;
	private boolean flag=false;
	// 生产的方法
	public synchronized void set(String name)
	{
		while(flag)
			try {
				this.wait();
			} catch (Exception e) {
				// TODO: handle exception
			}
		this.name=name+"-生产者-"+count++;
		System.out.println(Thread.currentThread().getName()+"生产者"+this.name);
		flag=true;
		this.notifyAll();
	}
	//  消费的方法
	public synchronized void out()
	{
		while(!flag)
			try {
				this.wait();
			} catch (Exception e) {
				// TODO: handle exception
			}
		System.out.println(Thread.currentThread().getName()+"消费者"+this.name);
		flag=false;
		this.notifyAll();
	}
}
//生产者  调用商品的生产方法
class PorduterDemo  implements Runnable
{
	private Good g;
	PorduterDemo(Good g)
	{
		this.g=g;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true)
		{
			g.set("商品");
		}
	}
}
//消费者调用  商品的消费方法
class ConsumerDemo  implements Runnable
{

	private Good g;
	ConsumerDemo(Good g)
	{
		this.g=g;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true)
		{
			g.out();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值