生产者:
package com.mary.product;
public class Producer implements Runnable {
private String name;
private Storage store;
public Producer(String name, Storage store) {
super();
this.name = name;
this.store = store;
}
@Override
public void run() {
while (true) {
try {
synchronized (store) {
if (store.isFull()) {
System.out.println("【" + name + "】发现仓库满了,等待消费者消费");
store.wait();
} else {
Product p = new Product("产品" + (int) (Math.random() * 1000), (int) (Math
.random() * 1000));
store.push(p);
System.out.println("【" + name + "】生产了一个产品【" + p.toString() + "】库存量:"
+ store.getList().size());
store.notifyAll();
}
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
消费者:
package com.mary.product;
public class Consumer implements Runnable {
private String name;
private int num;
private Storage store;
public Consumer(String name, int num, Storage store) {
super();
this.name = name;
this.num = num;
this.store = store;
}
@Override
public void run() {
while (true) {
try {
synchronized (store) {
if (store.isEmpty() || store.size() < num) {
System.out.println("【" + name + "】需要[" + num + "]发现库存量不足,等待生产者生产");
store.wait();
} else {
for (int i = 0; i < num; i++) {
store.pop();
}
System.out.println("【" + name + "】消费了" + num + "个产品"
+ store.getList().size());
store.notifyAll();
}
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
产品:
package com.mary.product;
public class Product {
private String name;
private int price;
public Product() {
super();
}
public Product(String name, int price) {
super();
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "产品名称:" + name + ",价格: " + price;
}
}
仓库:
package com.mary.product;
import java.util.ArrayList;
import java.util.List;
public class Storage {
private List<Product> list;
public final int max = 10;
public Storage() {
super();
list = new ArrayList<Product>();
}
public synchronized void push(Product p) {
list.add(p);
}
public synchronized void pop() {
list.remove(0);
}
public synchronized boolean isEmpty() {
return list.size() == 0 ? true : false;
}
public synchronized boolean isFull() {
return list.size() == max ? true : false;
}
public synchronized List<Product> getList() {
return list;
}
public int size() {
return list.size();
}
}
测试:
package com.mary.product;
public class Market {
public static void main(String[] args) {
Storage store = new Storage();
Producer p1 = new Producer("生产者1号", store);
Producer p2 = new Producer("生产者2号", store);
Producer p3 = new Producer("生产者3号", store);
Consumer c1 = new Consumer("消费者1号", 1, store);
Consumer c2 = new Consumer("消费者2号", 2, store);
Consumer c3 = new Consumer("消费者3号", 3, store);
new Thread(p1).start();
new Thread(p2).start();
new Thread(p3).start();
new Thread(c1).start();
new Thread(c2).start();
new Thread(c3).start();
}
}
运行结果:
源码下载地址:http://download.csdn.net/detail/yeruowei/6878003
如果有异议,欢迎各位批评指正。