
为什么在生产者-消费者问题中只有一个消费者参与消费?
在您提供的生产者-消费者示例中,出现了以下问题:只有单个消费者参与消费,而有多个消费者线程。
原因:
问题源自 wait() 和 notifyall() 方法的行为。
- list.wait() 只能由持有 list 锁的线程调用。
- list.notifyall() 只会唤醒所有因调用 list 锁的 wait() 而进入等待状态的线程。
因此,您的程序执行如下:
- 生产者和消费者竞争获取 list 锁。假设生产者首先获得锁。
- 生产者处理完数据后,调用 notifyall() 唤醒所有等待线程。
- 消费者 1 和 消费者 2 竞争获取锁。假设消费者 1 获取锁。
- 消费者 1 处理完数据后,调用 notifyall(),但只唤醒生产者,因为只有生产者在调用 list.wait() 后进入等待状态。
- 生产者被唤醒,处理完数据后再次调用 notifyall(),但仍然只唤醒消费者 1。
如此循环,导致只有消费者 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();
}
}










