日期:2014-05-20 浏览次数:20674 次
class Product {
private int productId = 0;
public Product(int productId) {
this.productId = productId;
}
public int getProductId() {
return productId;
}
public String toString() {
return Integer.toString(productId);
}
}
class StoreHouse {
private int base = 0;
private int top = 0;
// private Product[] products = new Product[10];
private LinkedList<Product> products=new LinkedList<Product>();
public synchronized void push(Product product) {
// while (top == products.length) {
while (products.size() == 10) {
notify();
try {
System.out.println("仓库已满,正等待消费...");
wait();
} catch (InterruptedException e) {
System.out.println("stop push product because other reasons");
}
}
// products[top] = product;
products.addLast(product);//add or addLast?
// top++;
}
public synchronized Product pop() {
Product pro = null;
// while (top == base) {
while (products.size() == 0) {
notify();
try {
System.out.println("仓库已空,正等待生产...");
wait();