模拟生产者-消费者问题的UNL图:
图3 生产者-消费者UML
图3为模拟生产者-消费者的UML图,Product类为生产者产生的产品类,Producer类为生产者,Consumer类为消费者,QueueMessage类用来存储多个生
产者产生产品以及提供多个消费者消费产品。Test类来测试“生产者-消费者”模拟系统。
3、模拟生产者-消费者问题的程序清单
package imut.cstd.j09_2.shiyan3;
public class Product { //产品类 private String pname; }
public String getPname(){ }
public void setPname(String pname){ }
this.pname = pname; return pname;
package imut.cstd.j09_2.shiyan3; import java.util.LinkedList;
public class QueueMessage { //共享区类 private static int num = 0;
private final static int MAX_num = 30;//上线商品
private LinkedList public boolean isEmpty(){ } public synchronized Product get(){ Product temp = null; if(queue.isEmpty()){ System.out.println(Thread.currentThread().getName()+\"消费者return queue.isEmpty(); \"+\":\"+\"目前没有可消费的商品!\"); try{ } else if(!queue.isEmpty()){ } this.wait(); System.out.println(e.getMessage()); }catch (InterruptedException e) { try { } System.out.println(Thread.currentThread().getName()+\"消费者 num--; temp = queue.poll(); this.wait(); System.out.println(e.getMessage()); } catch (InterruptedException e) { \"+\"\消费的商品号:\"+temp.getPname()+\"\目前商品的数量是:\"+num); } } public synchronized void add(Product temp){ if(num this.queue.add(temp); System.out.println(Thread.currentThread().getName()+\"生产者this.notifyAll(); return temp; \"+\"\产生的商品号:\"+temp.getPname()+\"\目前商品的数量是:\"+num); } else if(num>MAX_num){ System.out.println(\"生产的商品的数量已经满足:\"+num+\"生产者 \"+Thread.currentThread().getName()+\"产生的商品存不了了!\"); try{ } } } this.notifyAll(); } this.wait(); e.printStackTrace(); }catch(InterruptedException e){ package imut.cstd.j09_2.shiyan3; public class Producer implements Runnable{ //生产者类 private QueueMessage queue ; public QueueMessage getQueue(){ return queue; } public void setQueue(QueueMessage queue){ this.queue = queue; } } public synchronized void run() { product.setPname(Thread.currentThread().getName()+\"--->\"+(i+1)+\"\\ } } queue.add(product); try{ } Thread.sleep(200); System.out.println(e.getMessage()); System.out.println(e.getMessage()); }catch(InterruptedException e){ }catch(NullPointerException e){ for(int i = 0;i<20;i++){ Product product = new Product(); t\"); package imut.cstd.j09_2.shiyan3; public class Consumer implements Runnable{ //消费者类 private QueueMessage queue = null; public QueueMessage getQueue(){ return queue; } public void setQueue(QueueMessage queue){ this.queue = queue; } public void run() { for(int i = 0;i<20;i++){ queue.get(); try{ Thread.sleep(200); System.out.println(e.getMessage()); }catch(InterruptedException e){ }catch(NullPointerException e){ } } } } System.out.println(e.getMessage()); package imut.cstd.j09_2.shiyan3; public class Test { public static void main(String[] args){ //测试类 QueueMessage queue = new QueueMessage() ; Producer producer = new Producer(); producer.setQueue(queue); Consumer consumer = new Consumer(); consumer.setQueue(queue); Thread p = new Thread(producer); Thread p1 = new Thread(producer); Thread p2 = new Thread(producer); p.setName(\"p\"); //定义了三个消费者 p1.setName(\"p1\"); p2.setName(\"p2\"); p.start();p1.start();p2.start(); Thread c = new Thread(consumer); Thread c1 = new Thread(consumer); c.setName(\"c\"); c1.setName(\"c1\");//定义了两个生产者 c.start();c1.start(); } } 因篇幅问题不能全部显示,请点此查看更多更全内容