zoukankan      html  css  js  c++  java
  • Java中的生产消费者问题

      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 }
  • 相关阅读:
    Vue中axios基础使用(一)_前端前端请求数据
    vue中使用font-awesome
    vue-cli 搭建项目中,img引用资源404
    前端工程化常用的基础lunix命令
    vue运行项目时network显示unavailable
    关于vue中node_modules中第三方模块的修改使用
    tableau extension 调研
    使用 certbot 自动给 nginx 加上 https
    前端常用:复制到剪切板和下载
    ssh 的一个坑
  • 原文地址:https://www.cnblogs.com/tianming18/p/10245061.html
Copyright © 2011-2022 走看看