Lesson_for_java_day20--java的多线程——生产者消费者模式(优化网上生产馒头的案例)

来源:互联网 发布:手机记工天软件 编辑:程序博客网 时间:2024/05/20 18:43

目的:生产者生产多少产品,消费者就消费多少产品,且生产和消费同时进行。


生产者消费者模式(网上生产馒头的案例):

package sonyi;/* * 生产者与消费者模型中,要保证以下几点:  * 1 同一时间内只能有一个生产者生产     生产方法加锁sychronized  * 2 同一时间内只能有一个消费者消费     消费方法加锁sychronized  * 3 生产者生产的同时消费者不能消费     生产方法加锁sychronized  * 4 消费者消费的同时生产者不能生产     消费方法加锁sychronized  * 5 共享空间空时消费者不能继续消费     消费前循环判断是否为空,空的话将该线程wait,释放锁允许其他同步方法执行  * 6 共享空间满时生产者不能继续生产     生产前循环判断是否为满,满的话将该线程wait,释放锁允许其他同步方法执行     * * 生产者消费者模式:对于多个生产者和消费者,为什么要用while判断标记:原因:让被唤醒的线程再一次判断标记。为什么定义notifyAll?因为需要唤醒对方线程。如果只用notify,容易出现只唤醒本方线程,导致程序中的所有线程都在等待。*/  //主类  class  ProducerConsumer  {      public static void main(String[] args)       {          StackBasket s = new StackBasket();          Producer p = new Producer(s);          Consumer c = new Consumer(s);          Thread tp = new Thread(p);          Thread tc = new Thread(c);          tp.start();          tc.start();      }  }   //产品类  class Mantou  {      private int id;            Mantou(int id){          this.id = id;      }       public String toString(){          return "Mantou :" + id;      }  }   //产品仓库 class StackBasket  {      Mantou sm[] = new Mantou[6];      int index = 0;            /**       * show 生产方法.      * show 该方法为同步方法,持有方法锁;      * show 首先循环判断满否,满的话使该线程等待,释放同步方法锁,允许消费;      * show 当不满时首先唤醒正在等待的消费方法,但是也只能让其进入就绪状态,      * show 等生产结束释放同步方法锁后消费才能持有该锁进行消费      * @param m 元素      * @return 没有返回值       */        public synchronized void push(Mantou m){          try{              while(index == sm.length){                  System.out.println("!!!!!!!!!生产满了!!!!!!!!!");                  this.wait();              }              this.notify();          }catch(InterruptedException e){              e.printStackTrace();          }catch(IllegalMonitorStateException e){              e.printStackTrace();          }                    sm[index] = m;          index++;          System.out.println("生产了:" + m + " 仓库有" + index + "个馒头");      }       /**       * show 消费方法      * show 该方法为同步方法,持有方法锁      * show 首先循环判断空否,空的话使该线程等待,释放同步方法锁,允许生产;      * show 当不空时首先唤醒正在等待的生产方法,但是也只能让其进入就绪状态      * show 等消费结束释放同步方法锁后生产才能持有该锁进行生产      * @param b true 表示显示,false 表示隐藏       * @return 没有返回值       */       public synchronized Mantou pop(){          try{              while(index == 0){                  System.out.println("!!!!!!!!!消费光了!!!!!!!!!");                  this.wait();              }              this.notify();          }catch(InterruptedException e){              e.printStackTrace();          }catch(IllegalMonitorStateException e){              e.printStackTrace();          }          index--;          System.out.println("消费了:---------" + sm[index] + " 仓库还有" + index + "个馒头");          return sm[index];      }  }   //生产类class Producer implements Runnable  {      StackBasket ss = new StackBasket();      Producer(StackBasket ss){          this.ss = ss;      }        public void run(){          for(int i = 1;i <= 20;i++){              Mantou m = new Mantou(i);              ss.push(m);  //          System.out.println("生产了:" + m + " 共" + ss.index + "个馒头");  //          在上面一行进行测试是不妥的,对index的访问应该在原子操作里,因为可能在push之后此输出之前又消费了,会产生输出混乱              try{                  Thread.sleep((int)(Math.random()*500));              }catch(InterruptedException e){                  e.printStackTrace();              }          }      }  }   //消费类class Consumer implements Runnable  {      StackBasket ss = new StackBasket();      Consumer(StackBasket ss){          this.ss = ss;      }       /**       * show 消费进程.      */       public void run(){       //《--问题--》当生产还没结束而消费已经将仓库内产品消费光时,消费线程(循环)就退出,当生产线程将仓库填满时就一直处于等待状态,没办法结束        while(ss.index != 0){             ss.pop();  //          System.out.println("消费了:---------" + m + " 共" + ss.index + "个馒头");  //  同上在上面一行进行测试也是不妥的,对index的访问应该在原子操作里,因为可能在pop之后此输出之前又生产了,会产生输出混乱              try{                  Thread.sleep((int)(Math.random()*1000));              }catch(InterruptedException e){                  e.printStackTrace();              }          }         System.out.println("消费结束");    }  } 
该案例在运行中有时会出现如下情况:


问题:仓库满后,消费线程却结束了,结果导致生产线程一直处于等待状态。程序没法结束。究其原因是因为消费线程在判断仓库产品为0之后就退出循环,结束消费线程,而不考虑生产线程是否仍在生产。所以当消费线程抢险运行时,就容易结束消费线程(案例中每生产或消费一个产品都会休眠一下,所以生产和消费交替运行,问题不明显,如果去掉休眠,很容易出现问题)。现将生产者消费者模式优化下,在消费线程中增加判断生产线程是否结束,这样就不会出现仓库已满,消费线程却已经结束的结果。


优化后的生产者消费者模式:

package sonyi;public class ProAndConDemo {public static void main(String[] args) {Stack stack = new Stack();new Thread(new Producer1(stack)).start();new Thread(new Consumer1(stack)).start();}}class Consumer1 implements Runnable{private Stack stack;public Consumer1(Stack stack) {this.stack = stack;}@Overridepublic void run() {while (Producer1.flag) {//创建标记,如果生产结束,消费才结束stack.pop();//消费产品try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("--------------消费结束--------------");}}class Producer1 implements Runnable{public static boolean flag = true;//创建生产标记,开始生产为true,结束生产为falseprivate Stack stack;public Producer1(Stack stack) {this.stack = stack;}@Overridepublic void run() {for(int i = 0; i < 20; i++){//生产20个产品Produce p = new Produce(i+1);//生产一个产品stack.push(p);//产品添加到仓库中try {Thread.sleep(1);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}flag = false;//结束生产标记System.out.println("----------生产结束--------------");}}class Stack{Produce[] storeHose = new Produce[5];//创建容量为5的仓库int index = 0;public synchronized void push(Produce p){//往仓库内存放产品(传入一个产品)try {while (index == storeHose.length) {//判断仓库是否已经满了System.out.println("仓库已经满了---------");this.wait();//仓库满时,生产等待}this.notifyAll();//唤醒其他线程} catch (InterruptedException e) {e.printStackTrace();}storeHose[index++] = p;//仓库添加一个产品System.out.println("生产了" + p.id + "号产品,现在仓库有:" + index + "产品");}public synchronized void pop(){//往仓库中消费产品try {//双重判断,只有当仓库没有产品和成产还在继续时,消费线程才进入等待。//也就是说,当生产还在继续时,仓库没有产品,消费线程才进入等待,当生产结束时,消费线程不会进入等待,而是将产品消费光while(index == 0 && Producer1.flag){System.out.println("产品已经消费完了---------");this.wait();}this.notifyAll();//消费线程等待时,唤醒其他线程} catch (InterruptedException e) {e.printStackTrace();}Produce p = storeHose[--index];//消费仓库里的一个产品System.out.println("消费了" + p.id + "号产品,现在仓库还有" + index + "产品");}}//创建产品类,每个产品都有自己的idclass Produce{int id;public Produce(int id) {this.id = id;}}

欢迎java学习者共同讨论。

0 0
原创粉丝点击