Java中的生产消费者问题

2019-01-10 07:48:24来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

  1 package day190109;
  2 
  3 import java.util.LinkedList;
  4 import java.util.Queue;
  5 import java.util.Random;
  6 
  7 public class 生产消费者ThreadDemo10 {
  8     public static void main(String[] args) {
  9         //生产消费者模式
 10         Queue<Phone> queue = new LinkedList<Phone>();
 11         Producer producer = new Producer(queue,30);
 12         Consumers consumers = new Consumers(queue,"李江萍");
 13         Consumers consumers1 = new Consumers(queue,"熊天明");
 14 
 15         producer.start();
 16         consumers.start();
 17         consumers1.start();
 18 
 19     }
 20 }
 21 class Producer extends Thread{
 22     public Queue<Phone> queue;
 23     public  int max;
 24     //构造方法
 25     public Producer(Queue<Phone> queue, int max) {
 26         this.queue = queue;
 27         this.max = max;
 28     }
 29 
 30     @Override
 31     public void run() {
 32         while (true){
 33            synchronized (queue) {
 34                try {
 35                    Thread.sleep(1000);
 36                } catch (InterruptedException e) {
 37                    e.printStackTrace();
 38                }
 39                if (queue.size() == max) { //如果生产的商品满啦
 40                    try {
 41                        System.out.println("你好!本地库存产品已满!请通知消费之前来消费");
 42                        queue.wait();//先不忙生产,让消费者先消费
 43                    } catch (InterruptedException e) {
 44                        e.printStackTrace();
 45                    }
 46                } else {
 47                    Random r = new Random();
 48                    Phone p = new Phone(r.nextInt(7), (double) r.nextInt(2000));
 49                    System.out.println("甲方已经生产了1个手机:" + p);
 50                    queue.offer(p);
 51                    System.out.println("      生产后现在的库存是:" + queue.size()+"个");
 52                    queue.notifyAll();//通知消费者。可以消费啦
 53                }
 54            }
 55         }
 56     }
 57 }
 58 class Consumers extends Thread{
 59     public Queue<Phone> queue;
 60     public String name;
 61     //构造方法
 62     public Consumers(Queue<Phone> queue, String name) {
 63         this.queue = queue;
 64         this.name = name;
 65     }
 66 
 67     @Override
 68     public void run() {
 69         while (true) {
 70             synchronized (queue) {
 71                 try {
 72                     Thread.sleep(1000);
 73                 } catch (InterruptedException e) {
 74                     e.printStackTrace();
 75                 }
 76                 if (queue.size() == 0) {
 77                     try {
 78                         System.out.println(this.name+"说:“你们本地库存产品严重不足”");
 79                         queue.wait();
 80                     } catch (InterruptedException e) {
 81                         e.printStackTrace();
 82                     }
 83                 } else {
 84                     Phone p = queue.poll();
 85                     System.out.println(this.name + "说:我要买1个手机:" + p);
 86                     System.out.println("      消费后现在的库存是:" + queue.size()+"个");
 87                     queue.notifyAll();//通知生产者可以生产了
 88                 }
 89             }
 90         }
 91     }
 92 }
 93 class Phone{
 94     private  Integer size;
 95     private Double price;
 96 
 97     public Phone(Integer size, Double price) {
 98         this.size = size;
 99         this.price = price;
100 
101     }
102 
103     @Override
104     public String toString() {
105         return "手机规格是{" +
106                 "尺寸是:" + size +
107                 ", 价格是:" + price +"元"+
108                 '}';
109     }
110 }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:javaSystem.out.println()输出byte[]和char[]异常的问题

下一篇:Java8 默认方法