package programingModel;
/*  * 生产者与消费者模型  * 多线程的使用  */
//主函数类 public class ProductorAndConsumer {
 public static void main(String[] args)  {   //定义存储数据的仓库   Storage storage=new Storage(100,0);      //生产者   Productor p1=new Productor(10,storage);   Productor p2=new Productor(50,storage);   Productor p3=new Productor(70,storage);   Productor p4=new Productor(20,storage);   Productor p5=new Productor(60,storage);   Productor p6=new Productor(40,storage);   Thread tp1=new Thread(p1);   Thread tp2=new Thread(p2);   Thread tp3=new Thread(p3);   Thread tp4=new Thread(p4);   Thread tp5=new Thread(p5);   Thread tp6=new Thread(p6);
  //消费者   Consumer c1=new Consumer(25,storage);   Consumer c2=new Consumer(60,storage);   Consumer c3=new Consumer(40,storage);   Consumer c4=new Consumer(10,storage);   Consumer c5=new Consumer(50,storage);   Consumer c6=new Consumer(20,storage);   Consumer c7=new Consumer(15,storage);   Consumer c8=new Consumer(30,storage);   Thread tc1=new Thread(c1);   Thread tc2=new Thread(c2);   Thread tc3=new Thread(c3);   Thread tc4=new Thread(c4);   Thread tc5=new Thread(c5);   Thread tc6=new Thread(c6);   Thread tc7=new Thread(c7);   Thread tc8=new Thread(c8);      //开始进程   tp1.start();   tp2.start();   tp3.start();   tp4.start();   tp5.start();   tp6.start();   tc1.start();   tc2.start();   tc3.start();   tc4.start();   tc5.start();   tc6.start();   tc7.start();   tc8.start();  } }
//存储模型 class Storage {  private int maxStorage;   //定义最大存储量  private int currentStorage;  //定义已使用的存储量    public Storage(int max,int cur)  {   maxStorage=max;   currentStorage=cur;  }  public int getMaxStorage() {   return maxStorage;  }  public void setMaxStorage(int maxStorage) {   this.maxStorage = maxStorage;  }  public int getCurrentStorage() {   return currentStorage;  }  public void setCurrentStorage(int currentStorage) {   this.currentStorage = currentStorage;  }  //接收来自生产者生产的产品  public boolean inProduct(int nProduct)  {   if(currentStorage+nProduct>maxStorage)   {    System.out.println("仓库已满!");    return false;   }   else   {    currentStorage+=nProduct;    System.out.println("仓库产品+"+nProduct+"  "+"仓库剩余产品:"+currentStorage);    return true;   }   /*   synchronized(currentStorage)   {    while(currentStorage+nProduct>maxStorage)    {     try {      currentStorage.wait();     } catch (InterruptedException e) {      e.printStackTrace();     }    }    currentStorage+=nProduct;    System.out.println("产品已进入仓库 数量:"+nProduct+"仓库已有数量"+getCurrentStorage());    currentStorage.notify();   }   */  }  //向消费者提***品  public boolean outProduct(int nConsume)  {   if(currentStorage<nConsume)   {    System.out.println("仓库数量不足!");    return false;   }   else   {    currentStorage-=nConsume;    System.out.println("仓库产品-"+nConsume+"  "+"仓库剩余产品:"+currentStorage);    return true;   }   /*   synchronized(currentStorage)   {    while(currentStorage<nConsume)    {     try {      currentStorage.wait();     } catch (InterruptedException e) {      e.printStackTrace();     }    }    currentStorage-=nConsume;    System.out.println("已从仓库中取出 数量:"+nConsume+"仓库剩余数量"+getCurrentStorage());    currentStorage.notify();   }*/  } } //生产者模型 class Productor implements Runnable {  private int produce;  //生产产品数量  private Storage storage; //生产产品存储到哪个仓库    public Productor(int produce,Storage storage)  {   this.produce=produce;   this.storage=storage;  }  //get set方法  public int getProduction() {   return produce;  }
 public void setProduction(int production) {   this.produce = production;  }
 public Storage getStorage() {   return storage;  }
 public void setStorage(Storage storage) {   this.storage = storage;  }  //生产者生产  public boolean product()  {   return storage.inProduct(produce);  }  //执行线程时所做处理  @Override  public void run() {   synchronized(storage)   {    while(!product())    {     try {      storage.wait();     } catch (InterruptedException e) {      e.printStackTrace();     }    }    storage.notifyAll();   }   /*   synchronized(storage)   {    while(!product())    {     System.out.println("产品未进入仓库 数量:"+produce+"仓库空间"+(storage.getMaxStorage()-storage.getCurrentStorage()));     try {      this.wait();     } catch (InterruptedException e) {     }    }    System.out.println("产品已进入仓库 数量:"+produce+"仓库已有数量"+storage.getCurrentStorage());    this.notifyAll();   }   */  /* product();*/  } } //消费者模型 class Consumer implements Runnable {  private int need;   //消费产品数量  private Storage storage; //获取产品数量的仓库    public Consumer(int need,Storage storage)  {   this.need=need;   this.storage=storage;  }  //get set 方法
 public int getNeed() {   return need;  }
 public void setNeed(int need) {   this.need = need;  }
 public Storage getStorage() {   return storage;  }
 public void setStorage(Storage storage) {   this.storage = storage;  }    //消费者消耗  public boolean consume()  {   return storage.outProduct(need);  }  @Override  public void run()  {   synchronized(storage)   {    while(!consume())    {     try {      storage.wait();     } catch (InterruptedException e) {      e.printStackTrace();     }    }    storage.notifyAll();   }   /*   synchronized(storage)   {    while(!consume())    {     System.out.println("未能从仓库取出  数量:"+need+"仓库存在数量"+storage.getCurrentStorage());     try {      this.wait();     } catch (InterruptedException e) {     }    }    System.out.println("已从仓库中取出 数量:"+need+"仓库剩余数量"+storage.getCurrentStorage());    this.notifyAll();   }*/   /*consume();*/  } }