JAVA实现生产者消费者问题

第一步,定义一个商品类。

package com.moon.thread;
/**
 * 
 * @author cenyuan
 * 商品类
 */
public class Product {
	
	private String name;
	private String id;
	
	public Product()
	{
		
	}
	public Product(String name)
	{
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
}
第二步,定义仓库类
package com.moon.thread;
/**
 * 
 * @author cenyuan
 * 仓库类
 */
public class Storage 
{
	private String name ;//仓库名字
	private String id;   //仓库编号
	
	private static final int MAX_STORAGE_NUM = 10;//仓库的最大容量
	private static final int MIN_STORAGE_NUM = 1 ;//仓库的最小容量
	
	private Product[] products;//商品集
	private int size=0; //商品集大小
	
	public Storage()
	{
		products = new Product[MAX_STORAGE_NUM];
	}
	
	public synchronized Product put(Product prod)
	{
		//System.out.println("MAX_STORAGE_NUM < size="+(MAX_STORAGE_NUM < size));
		if(MAX_STORAGE_NUM < size)
		{
			try 
			{
				System.out.println(Thread.currentThread()+"等待消费者消费!");
				this.wait();//如果商品集已经超过仓库的容量则生产者线程等待
			} catch (InterruptedException e) 
			{
				System.out.println("当生产产品" + prod.getName()+"出现错误");
				e.printStackTrace();
			}
		}
		else
		{
			System.out.println("开始生产" + prod.getName());
			products[size] = prod;
			size++;
			
			System.out.println("仓库里面有 = " +size + "产品");
			this.notifyAll();
		}
		return prod;
	}
	
	public synchronized Product get()
	{
		
		Product prod = null;
		//System.out.println("MIN_STORAGE_NUM > size : " + MIN_STORAGE_NUM +">" +size);
		if(MIN_STORAGE_NUM > size)
		{
			try 
			{
				System.out.println(Thread.currentThread()+"等待生产者生产!");
				this.wait();//如果商品集已经小于仓库的最小容量则消费者线程等待
			} catch (InterruptedException e) 
			{
				System.out.println("当消费产品时出现错误");
				e.printStackTrace();
			}
		}
		else
		{
		size--;
		prod = products[size];
		//System.out.println("开始消费" + prod.getName());
		
		System.out.println(Thread.currentThread()+"已消费,现在仓库里面有 = " +size + "产品");
		this.notifyAll();
		}
		return prod;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
}
 第三步,定义生产者类
package com.moon.thread;
/**
 * 
 * @author cenyuan
 * 生产者类
 */
public class Producter implements Runnable{

	private String name;
	private Storage storage;
	
	public void create() throws InterruptedException
	{
		for(int i=0;;i++)
		{
			Thread.sleep(800);
		//	System.out.println("Producter Current Thread : " + Thread.currentThread());
			
			Product prod = new Product("Book "+i);
		//	System.out.println(name + "准备生产产品:"+prod.getName());
			storage.put(prod);
		}
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Storage getStorage() {
		return storage;
	}
	public void setStorage(Storage storage) {
		this.storage = storage;
	}
	public Producter(String name,Storage storage)
	{
		this.storage = storage;
		this.name = name;
	}
	@Override
	public void run() {
		try {
			create();
		} catch (InterruptedException e) {
			System.out.println("生产时出现错误!");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
 第四步,定义一个消费者类
package com.moon.thread;
/**
 * 
 * @author cenyuan
 * 消费者类
 */
public class Customer implements Runnable{

	private String name;
	private Storage storage;
	
	public void cust() throws InterruptedException
	{
		while(true)
		{
			Thread.sleep(500);
			//System.out.println("Customer Current Thread : " + Thread.currentThread());
			//System.out.println(name + "准备消费产品:");
			Product prod = storage.get();
			if(prod!=null)
			System.out.println(name + "消费产品:"+prod.getName()+" 结束");
		}
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Storage getStorage() {
		return storage;
	}
	public void setStorage(Storage storage) {
		this.storage = storage;
	}
	public Customer(String name,Storage storage)
	{
		this.name = name;
		this.storage = storage;
	}
	@Override
	public void run() {
		try {
			cust();
		} catch (InterruptedException e) {
			System.out.println("消费时出现错误!");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
 第五步 定义一个测试类
package com.moon.thread;

public class Tester {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Storage storage = new Storage();
		
		Producter proder1 = new Producter("王杰",storage);
		Producter proder2 = new Producter("蒋东",storage);
		
		Customer cu1 = new Customer("胡株",storage);
		Customer cu2 = new Customer("何德",storage);
		Customer cu3 = new Customer("张三",storage);
		
		Thread thread1 = new Thread(proder1,"王杰"); 
		Thread thread2 = new Thread(cu3,"张三"); 
		Thread thread3 = new Thread(cu1,"胡株"); 
		Thread thread4 = new Thread(cu2,"何德"); 
		
		thread3.start();
		thread1.start();
		thread2.start();
		
		thread4.start();
		
		System.out.println("Main thread end !");

	}

}
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值