在您提供的生产者-消费者示例中,出现了以下问题:只有单个消费者参与消费,而有多个消费者线程。
原因:
问题源自 wait() 和 notifyall() 方法的行为。
因此,您的程序执行如下:
如此循环,导致只有消费者 1 能够参与消费。
解决方法:
要解决此问题,可以使用一个额外的标志来指示何时唤醒生产者,以及何时唤醒消费者。
例如:
private boolean isconsumerwaiting = false; synchronized (list) { while (true) { // 如果没有消费者等待并且 list 大小为 0,则等待 if (!isconsumerwaiting && list.size() == 0) { try { list.wait(); } catch (interruptedexception e) { e.printstacktrace(); } } // 否则,生产数据 object object = new object(); list.add(object); system.out.println(thread.currentthread().getname() + "生产 list" + object); // 设置消费者等待标志为 true isconsumerwaiting = true; // 唤醒所有消费者 list.notifyall(); } }
在消费线程中:
synchronized (list) { while (true) { // 如果没有可消费的数据且消费者等待,则等待 if (list.size() == 0 && isConsumerWaiting) { try { list.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // 否则,消费数据 Object object = list.remove(0); System.out.println(Thread.currentThread().getName() + "消费 list" + object); // 重置消费者等待标志为 false isConsumerWaiting = false; // 唤醒所有生产者 list.notifyAll(); } }
以上就是生产者-消费者问题中为什么只有一个消费者参与消费?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号