原野码农 2023-02-16 17:32 采纳率: 100%
浏览 30
已结题

多线程生产者消费者模型中,多个生产者与一个消费者,生产超过限定容量。

在编辑多线程的生产者-消费者模型的过程中,发现执行结果不符合理想结果,超出了限制。
1、问题:超过仓库容量,生产者继续生产。
2、代码:

public class producerAndConsumer {

    public static void main(String[] args) {
        LinkedList<Integer> list= new LinkedList<Integer>();
        MessageQueue queue = new MessageQueue(list,3);//仓库队列,容量设置为3
        //创建并启动3个生产的线程
        for (int i = 1; i <= 3; i++) {
            new Thread(()->{
                int obj = 0;
                int j=0; //控制次数
                while(j<10){
                    j++;
                    queue.producer(obj);
                    obj++;
                }
            },"producer"+i).start();
        }
        //创建并启动消费的线程
        new Thread(()->{
            int j=0;  //控制次数
            while(j<10){
                j++;
                queue.consumer();
            }
        },"consumer").start();
    }
    /**
     * 生产消费队列
     */
    static class MessageQueue{
        private LinkedList<Integer> list;
        private int capacity;  //容量
        //生产
        public void producer(Integer obj){
            synchronized (this.list){
                if(list.size()==capacity){
                    try {
                        System.out.println("仓库已满-------------->停止生产!");
                        list.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                list.add(obj);
                System.out.println(Thread.currentThread().getName()+"生产:"+obj+",size:"+list.size()+"--仓库:"+list);
                list.notifyAll();
            }
        }
        //消费方法
        public Integer consumer(){
            synchronized (this.list){
                if(list.isEmpty()){
                    try {
                        System.out.println("仓库已空-------------->停止消费!");
                        list.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                Integer obj = list.remove();
                System.out.println(Thread.currentThread().getName()+"消费:"+obj+",size:"+list.size()+"--仓库:"+list);
                list.notifyAll();
                return obj;
            }
        }
        //构造方法
        public MessageQueue(LinkedList<Integer> list,int capacity){
            this.list = list;
            this.capacity = capacity;
        }
    }
}

3、执行结果:

img

  • 写回答

4条回答 默认 最新

  • Tomshidi 2023-02-16 18:00
    关注

    用我这份试试

    public class ProducerAndConsumer {
    
        public static void main(String[] args) {
            LinkedList<Integer> list= new LinkedList<Integer>();
            MessageQueue queue = new MessageQueue(list,3);//仓库队列,容量设置为3
            //创建并启动3个生产的线程
            for (int i = 1; i <= 3; i++) {
                new Thread(()->{
                    int obj = 0;
                    int j=0; //控制次数
                    while(j<10){
                        j++;
                        queue.producer(obj);
                        obj++;
                    }
                },"producer"+i).start();
            }
            //创建并启动消费的线程
            new Thread(()->{
                int j=0;  //控制次数
                while(j<10){
                    j++;
                    queue.consumer();
                }
            },"consumer").start();
        }
        /**
         * 生产消费队列
         */
        static class MessageQueue{
            private volatile LinkedList<Integer> list;
            private int capacity;  //容量
            //生产
            public void producer(Integer obj){
                synchronized (this.list){
                    while (list.size()>=capacity){
                        try {
                            System.out.println("仓库已满-------------->停止生产!");
                            list.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    list.add(obj);
                    System.out.println(Thread.currentThread().getName()+"生产:"+obj+",size:"+list.size()+"--仓库:"+list);
                    list.notifyAll();
                }
            }
            //消费方法
            public Integer consumer(){
                synchronized (this.list){
                    while (list.isEmpty()){
                        try {
                            System.out.println("仓库已空-------------->停止消费!");
                            list.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    Integer obj = list.remove();
                    System.out.println(Thread.currentThread().getName()+"消费:"+obj+",size:"+list.size()+"--仓库:"+list);
                    list.notifyAll();
                    return obj;
                }
            }
            //构造方法
            public MessageQueue(LinkedList<Integer> list,int capacity){
                this.list = list;
                this.capacity = capacity;
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 2月25日
  • 已采纳回答 2月17日
  • 创建了问题 2月16日

悬赏问题

  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。