生产者与消费者

死锁

Storage仓库

package com.storage;
//仓库类

import java.util.ArrayList;

public class Storage {
	//最大库存量
	private static final int MAX_SIZE=1000;
	//仓库载体(数组)
	private ArrayList<Object> list=new ArrayList<>();
	
	//生产产品
	public void product(int num) {
		//同步锁!!!一次只能进入一个线程
		//参数:线程想要操作的对象
		synchronized(list) {
			while (num+list.size()>MAX_SIZE) {
				System.out.println("生产的数量"+num+"\t库存量"+list.size()
				+"\t 仓库空间不足,无法生产");//  \t转义字符 4个空格
				
				//线程等待!!!
				try {
					list.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			//说明 库存是够的
			for(int i=0;i<num;i++) {
				Object o=new Object();
				list.add(o);
			}
			System.out.println("生产的数量:"+num+"\t库存量:"+
			list.size());
			
			//关键 唤醒线程
			list.notifyAll();//唤醒目前在等待的所有线程
			
		}
		
	}
	
	//消费产品
	public void consume(int num) {
		synchronized(list) {
			while(num>list.size()) {
				System.out.println("消费的数量:"+num+"\t库存量:"+
			list.size()+"\t库存不足无法消费!");
				//线程等待!!!
				try {
					list.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			//说明库存是充裕
			for (int i = 0; i < num ;i++) {
				list.remove(0);//数组中有空缺 后面的元素会补上
			}
			System.out.println("消费的数量:"+num+"\t库存量"+list.size());
			//唤醒线程
			list.notifyAll();
			
		}
	}
	
}

Consumer消费者

package com.storage;

public class Consumer extends Thread{
	//仓库
	private Storage storage;
	private int num;
	public Consumer(Storage storage, int num) {
		this.storage = storage;
		this.num = num;
	}
	
	@Override
	public void run() {
		//消费产品
		storage.consume(num);
	}
	
}

Producter生产者

package com.storage;

public class Producter extends Thread {
	//仓库属性
	private Storage storage;
	//生产的数量
	private int num;
	//构造方法 用于给属性赋值
	public Producter(Storage storage,int num) {
		this.storage=storage;
		this.num=num;
	}
	
	@Override
	public void run() {
		storage.product(num);
	}
	
	
}

TestMain测试类

package com.storage;

public class TestMain {

	public static void main(String[] args) {
		//新建仓库
		Storage storage=new Storage();
		
		//生产者
		Producter p1=new Producter(storage, 200);
		Producter p2=new Producter(storage, 300);
		Producter p3=new Producter(storage, 400);
		Producter p4=new Producter(storage, 500);
		Producter p5=new Producter(storage, 600);
		
		//消费者
		Consumer c1=new Consumer(storage, 100);
		Consumer c2=new Consumer(storage, 200);
		Consumer c3=new Consumer(storage, 100);
		Consumer c4=new Consumer(storage, 300);
		Consumer c5=new Consumer(storage, 400);
		
		
		p1.start();
		p2.start();
		p3.start();
		p4.start();
		p5.start();
		
		c1.start();
		c2.start();
		c3.start();
		c4.start();
		c5.start();
	}

}

我感觉核心就是消费者每次消费的不能超过仓库中的库存,超过了就用死锁锁住该线程,生产者每次生产的产品不能超过仓库存储的总库存,因为多了的话存不下,所以要上一把锁,等消费者消耗了库存然后在存入;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值