首页 > 编程语言 > 详细

生产者消费者多线程练习过程中遇到的bug

时间:2018-01-12 16:21:46      阅读:250      评论:0      收藏:0      [点我收藏+]
package threadStudy1;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName: ProducerConsumer     
 * @author yudexiao-lhq
 * @date 2018-1-12 上午11:15:29 
 * 对于此模型,应该明确一下几点:
    1、生产者仅仅在仓储未满时候生产,仓满则停止生产。
    2、消费者仅仅在仓储有产品时候才能消费,仓空则等待。
    3、当消费者发现仓储没产品可消费时候会通知生产者生产。
    4、生产者在生产出可消费产品时候,应该通知等待的消费者去消费。
 */

public class ProducerConsumer {
    public static void main(String[] args) {
        ProducerConsumer pc = new ProducerConsumer();
        Storage s = pc.new Storage();
        ExecutorService service = Executors.newCachedThreadPool();
        Producer p = pc.new Producer("张三", s);
        Producer p2 = pc.new Producer("李四", s);
        Consumer c = pc.new Consumer("lily", s);
        Consumer c2 = pc.new Consumer("rose", s);
        Consumer c3 = pc.new Consumer("tracy", s);
        Consumer c4 = pc.new Consumer("dany", s);
        service.submit(p);
        service.submit(p2);
        service.submit(c);
        service.submit(c2);
        service.submit(c3);
    }
    
    /**
     * @ClassName: Producer     
     * @author yudexiao-lhq
     * @date 2018-1-12 上午11:16:02 
     */
    class Producer implements Runnable{
        private String name;
        private Storage s = null;
        
        public Producer(String name, Storage s) {
            this.name = name;
            this.s = s;
        }
        @Override
        public void run() {
            try {
                while(true){
                    System.out.println(name+"-准备生产");
                    Product pro = new Product((int)Math.ceil(Math.random()*10));
                    //Product pro = new Product((int)Math.ceil(Math.random()*1000000000));
                    System.out.println("===========已成功生产:"+pro.toString());
                    s.add(pro);
                    System.out.println("===========已成功入库:"+pro.toString());
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    class Consumer implements Runnable{
        private String name;
        private Storage s = null;
        public Consumer(String name, Storage s) {
            this.name = name;
            this.s = s;
        }
        public void run() {
            try {
                while(true){
                    System.out.println("start to consume...");
                    Product pro = s.remove();
                    System.out.println(name+"===========已消费产品:"+pro.toString());
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * @ClassName: Storage     
     * @author yudexiao-lhq
     * @date 2018-1-12 上午11:16:02 
     */
    public class Storage {
        BlockingQueue<Product> queue = new ArrayBlockingQueue<Product>(10);
        //入库
        public void add(Product p) throws InterruptedException {
            //queue.put(p);
            queue.put(p);
            //System.out.println("当前库存;"+queue.size());
        }
        //出库
        public Product remove() throws InterruptedException {
            return queue.take();
        }
    }
    /**
     * @ClassName: Product     
     * @author yudexiao-lhq
     * @date 2018-1-12 上午11:16:02 
     */
    public class Product {
        private int id;
        public Product (int id){
            this.id = id;
        }
        @Override
        public String toString() {
            return "["+id+"]号产品";
        }
    }

}

Product pro = new Product((int)Math.ceil(Math.random()*10));中如果随机数取值太小,会出现产品号重复的问题,所以说不要再低端问题上掉进坑中,故而使用Product pro = new Product((int)Math.ceil(Math.random()*1000000000));取而代之。

技术分享图片

bug截图复现如上图。

生产者消费者多线程练习过程中遇到的bug

原文:https://www.cnblogs.com/ydx-1122/p/8276355.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!