消费和生产 二条线程同步 的例子



//属性类
public class Food {
	//菜名
	private String name;
	//构造方法  方便造菜单
	public Food(String name){
		super();
		setName(name);
	}
	//重写toString  方便打印集合
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "食物:"+getName();
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
//跟据   菜单   生产  消费   类
public class KFC {
	public static final String[] names = {"薯条","汉宝宝","可乐","鸡腿"};
	
	public static final int MAX = 10;
	
	public static final int MIN = 5;
	
	List
   
   
    
     foods = new ArrayList<>();
	//生产方法			参数是厨师一次生产多少个
	public void  yield(int index){
		//同步锁定 this 食材对象
		synchronized(this){
			//当食物大于20个时  等待被唤醒
			while (foods.size() > MIN) {
				System.out.println("食材够了");
				try {
					//等待语句  等待中
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			//当小于或等于10时  开始生产
			//当库存个数加生产个数    小于10时   开始生产
			if((index+foods.size())< MAX){
				System.out.println("开始生产");
				//每次按照菜单   随机生产第几个品种     
				Food food = new Food(names[(int)(Math.random() * 4)]);
				//厨师一次可以做index 个
				for (int j = 0; j < index; j++) {
						System.out.println("生产了一个"+food.getName());
						//每生产一个品种增加到库存
						foods.add(food);
				}
			}
			this.notifyAll();
		}
	}

	//浪费方法			参数一个顾客吃几个
	public void waste(int index){
		//同步锁住 this 食材对象
		synchronized (this) {
			//库存小于顾客要吃的情况
			while (foods.size() < index) {
				System.out.println("没有做好的,请等待");
				try {
					//等待语句
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			//顾客可以吃了
			System.out.println("开始卖出");
			//卖出一个从库存删一个
			for (int i = 0; i < index; i++){
				//注意这里要从后面开始删
				Food food = foods.remove(foods.size()-1);
				System.out.println("卖出了:"+food.getName());
			}
			this.notifyAll();
		}
	}



}

   
   
//厨师生产 
public class Waiter extends Thread {
	//厨师根据 菜单生产
	KFC kfc;
	//构造方法	给引用赋值
	public Waiter(KFC kfc){
		super();
		this.kfc = kfc;
	}
	//重写run方法
	@Override
	public void run() {
		//厨师一次生产5到7个
		int size = (int)(Math.random()*5+2);
		while (true) {
			kfc.yield(size);
		}
	}
}
//顾客 线程类
public class Customers extends Thread {
	//给顾客  引路来  去KFC 消费   这并没有给它地址
	KFC kfc;
	
	public Customers(KFC kfc){
		super();
		//给引路来的给它一个地址
		this.kfc = kfc;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		while (true) {
			//顾客一次买4到5个
			int size = (int) (Math.random() * 4) + 1;
			kfc.waste(size);
		}		
	}
	
	
}

public class Test {

	public static void main(String[] args) {
		/*
		 *  需求 KFC
		 * 
		 *  顾客 消费食物 消费 不够了 等待服务器去做  服务员 生成食物 做满20个 不做了 生产20个
		 */
		//实例化门店
		KFC kfc = new KFC();
		//实例化厨师
		Waiter w1 = new Waiter(kfc);
		Waiter w2 = new Waiter(kfc);
		//实例化顾客
		Customers c1 = new Customers(kfc);
		Customers c2 = new Customers(kfc);
		Customers c3 = new Customers(kfc);
		Customers c4 = new Customers(kfc);
		Customers c5 = new Customers(kfc);
		//调用线程
		w1.start();
		w2.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、付费专栏及课程。

余额充值