使用wait/notify实现生产者/消费者模式遇到的问题,以及求详解notify和notifyAll
生产者/消费者模式是进程与线程学习中相当经典的问题,在本文中场景设置如下:
有一个仓库(Repository),有若干个生产者(Producer)和若干个消费者(Consumer),生产者可以生产产品(Procuct)并存入(push)仓库,消费者可以从仓库取出(pop)并消费产品。
我希望通过使用synchronize锁机制与wait()/notifyAll()或notify()方法实现,代码如下。但实际测试中发现,当使用notify方法时,线程仅会通知最近使用的线程,时间一长就会变成仅有两个线程在实际运行。而使用notifyAll()方法时,仓库实际产品最大数量仅是创建的生产者的最大数量,却不能达到自己预先定义的最大数量,且线程执行顺序似乎并非随机的而是按照某一固定的顺序去执行。此处暂未想通。目测估计是我的线程唤醒方法调用的位置或者调用的方式有问题,正在努力解决中。求各位大佬帮忙解答。
代码如下:
import java.util.ArrayList;
/**
* 多生产者多消费者单仓库的生产者消费者模式
* @author breezefaith
*
*/
class Product{
private int productId;
public Product(int productId) {
super();
this.productId = productId;
}
@Override
public String toString() {
return "Product["+productId+"]";
}
}
class Repository{
private int maxSize;
private ArrayList<Product> list;
public Repository(int maxSize) {
super();
this.maxSize = maxSize;
list=new ArrayList<>(maxSize);
}
public void setMaxSize(int maxSize) {
if(maxSize<=list.size()) {
System.err.println("New size is smaller than the original.");
return;
}
ArrayList<Product> newList=new ArrayList<>(maxSize);
newList.addAll(list);
list=newList;
}
public int getMaxSize() {
return maxSize;
}
public int getCurrentSize() {
return list.size();
}
public void push(Product product) {
list.add(product);
}
public Product pop() {
return list.remove(0);
}
}
class Producer extends Thread{
private Repository repository;
private static int productId;
public Producer(Repository repository) {
super();
this.repository = repository;
}
public Product produce() {
Product product=new Product(productId++);
System.out.println("生产者 "+Thread.currentThread().getName()+" 生产了产品 "+product+",剩余:"+(repository.getCurrentSize()+1));
return product;
}
@Override
public void run() {
synchronized (repository) {
try {
while(true) {
Thread.sleep(1000);
if(repository.getCurrentSize()<repository.getMaxSize()) {
repository.push(produce());
}else {
System.err.println("Producer "+Thread.currentThread().getName()+" want to produce a product, but repository is full.");
}
repository.notifyAll();
repository.wait();
}
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer extends Thread{
private Repository repository;
public Consumer(Repository repository) {
super();
this.repository = repository;
}
public Product consume() {
Product product=repository.pop();
System.out.println("消费者 "+Thread.currentThread().getName()+" 消费了产品 "+product+",剩余:"+repository.getCurrentSize());
return product;
}
@Override
public void run() {
synchronized (repository) {
try {
while(true) {
Thread.sleep(1000);
if(repository.getCurrentSize()>0) {
consume();
}else {
System.err.println("Consumer "+Thread.currentThread().getName()+" consume a product, but repository is empty.");
}
repository.notifyAll();
repository.wait();
}
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Run {
public static void main(String[] args) {
Repository repository=new Repository(3);
Producer p1=new Producer(repository),
p2=new Producer(repository),
p3=new Producer(repository);
p1.setName("p1");
p2.setName("p2");
p3.setName("p3");
Consumer c1=new Consumer(repository),
c2=new Consumer(repository);
c1.setName("c1");
c2.setName("c2");
p1.start();
p2.start();
p3.start();
c1.start();
c2.start();
}
}
实验结果之一:
生产者 p1 生产了产品 Product[0],剩余:1
消费者 c1 消费了产品 Product[0],剩余:0
Consumer c2 consume a product, but repository is empty.
生产者 p3 生产了产品 Product[1],剩余:1
生产者 p2 生产了产品 Product[2],剩余:2
生产者 p3 生产了产品 Product[3],剩余:3
消费者 c2 消费了产品 Product[1],剩余:2
消费者 c1 消费了产品 Product[2],剩余:1
生产者 p1 生产了产品 Product[4],剩余:2
消费者 c1 消费了产品 Product[3],剩余:1
消费者 c2 消费了产品 Product[4],剩余:0
生产者 p3 生产了产品 Product[5],剩余:1
生产者 p2 生产了产品 Product[6],剩余:2
生产者 p3 生产了产品 Product[7],剩余:3
消费者 c2 消费了产品 Product[5],剩余:2
消费者 c1 消费了产品 Product[6],剩余:1
生产者 p1 生产了产品 Product[8],剩余:2
相关推荐
Java多线程中wait、notify、notifyAll使用详解
主要介绍了Java多线程中wait、notify、notifyAll使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
如何在Java中正确使用 wait, notify 和 notifyAll
wait, notify 和 notifyAll,这些在多线程中被经常用到的保留关键字,在实际开发的时候很多时候却并没有被大家重视。本文对这些关键字的使用进行了描述。 在 Java 中可以用 wait、notify 和 notifyAll 来实现线程间的通信。。举个例子,如果你的Java程序中有两个线程——即生产者和消费者,那么生产者可以通知消费者,让消费者开始消耗数据,因为队列缓冲区中有内容待消费(不为空)。相应的,消费者可以通知生产者可以开始生成更多的数据,因为当它消耗掉某些数据后缓冲区不再为满。 我们可以利用wait()来让一个线程在某些条件下暂停运行。例如,在生产者消费者模
Java 中Object的wait() notify() notifyAll()方法使用
主要介绍了Java 中Object的wait() notify() notifyAll()方法使用的相关资料,需要的朋友可以参考下
生产者消费者问题 Java实现
生产者消费者问题 Java实现 线程同步 线程通信生产者消费者问题 Java实现 线程同步 线程通信生产者消费者问题 Java实现 线程同步 线程通信
Object类wait及notify方法原理实例解析
主要介绍了Object类wait及notify方法原理实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下


Java SE
