package work8;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
class Food{
private String name="";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Food(String name) {
this.name=name;
}
}
class WorkRoom{
static final int Maxstore=5;
List store=new ArrayList();
private int getFoodStoreAmount(String name) {
if(store==null || store.size()==0) return 0;
int count=0;
Iterator iterator=store.iterator();//为什么在这里前面已经判断了,后面这里还需要遍历一遍?
while(iterator.hasNext()) {
if(iterator.next().getName().equals(name)) count++;//这一句是什么意思?没看太明白
}
return count;
}
//获取当前参数name的库存量,为什么前面都已经获取了一次,这里还需要获取一次呢?
private int getFoodIndex(String name){
if(store==null||store.size()==0) return -1;//为什么这里返回的是-1,前面获取的时候返回的是0?为什么有什么区别吗?
for(int i=0;i<store.size();i++)
if(store.get(i).getName().equals(name)) return i;//这一句是什么意思?没看太明白
return -1;
}
//食品生产
public void produce(String foodName) {
//下面synchronized这一条是什么意思?
synchronized(store) {
int foodCount=getFoodStoreAmount(foodName);
if(foodCount>=WorkRoom.Maxstore) {
System.out.println("【"+foodName+"】的库存量【"+foodCount+"】件,最高库存!");
store.notifyAll();//这里为什么需要唤醒其他的生产者和消费者
//这下面是停止生产的意思吗?wait他就是变成等待了是吗?
try {
store.wait();
}catch(InterruptedException e) {
e.printStackTrace();
}
}else {
Food food=new Food(foodName);
store.add(food);
store.notifyAll();
System.out.println("【"+foodName+"】已生产,目前库存量【"+(foodCount+1)+"】件!");
}
}
}
public void cosume(String foodName) {
synchronized(store){
//为什么又要用到上面定义过的这个?foodIndex,上面不是写了一次foodName了吗?为什么要写这个?
int foodIndex=getFoodIndex(foodName);
if(foodIndex<0) {
System.out.println("【"+foodName+"】的库存量【0】件,需要生产!");
store.notifyAll();
try {
store.wait();
}catch(InterruptedException e) {
e.printStackTrace();
}
}else {
Food food=store.get(foodIndex);
System.out.println("卖出一份【"+foodName+"】的食物!");
store.remove(food);
store.notifyAll();
}
}
}
}
//生产销售门店
class Producer implements Runnable{
private WorkRoom workRoom=null;
public Producer(WorkRoom room) {
this.workRoom=room;
}
@Override
public void run() {
while(true) {
Random rand=new Random();//这一句是什么意思??
int randIndex=rand.nextInt(4);//下面不是添加了三个属性吗?为什么这里添的是4?
if(randIndex==1) workRoom.produce("鸡腿");
if(randIndex==2) workRoom.produce("薯条");
if(randIndex==3) workRoom.produce("可乐");
try {
Thread.sleep(10000);
}catch(InterruptedException e) {
e.printStackTrace();
System.out.println("程序生产中断!");
}
}
}
}
class Customer implements Runnable{
private WorkRoom workRoom=null;
public Customer(WorkRoom room) {
this.workRoom=room;
}
@Override
public void run() {
while(true) {
Random rand=new Random();
int randIndex=rand.nextInt(4);
if(randIndex==1) workRoom.cosume("鸡腿");
if(randIndex==2) workRoom.cosume("薯条");
if(randIndex==3) workRoom.cosume("可乐");
try {
Thread.sleep(1000);
}catch(InterruptedException e) {
e.printStackTrace();
System.out.println("程序生产中断!");
}
}
}
}
public class runMain {
public static void main(String[] args) {
WorkRoom room=new WorkRoom();
Customer cust=new Customer(room);//这里以及下面的为什么要带这些参数,为什么不带参数他就报错?
Producer pro=new Producer(room);
Thread tCustomer=new Thread(cust);
tCustomer.start();
Thread tProducer=new Thread(pro);
tProducer.start();
}
}