public 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);
}
}
public class StoreHouse {
int base = 0;
int top = 0;
Product[] products = new Product[10];
public synchronized void push(Product product) {
while (top == products.length) {
notify();
try {
System.out.println("仓库已满,正等待消费...");
wait();
} catch (InterruptedException e) {
}
}
products[top] = product;
top++;
}
public synchronized Product pop() {
Product pro = null;
while (top == base) {
notify();
try {
System.out.println("仓库已空,正等待生产...");
wait();
} catch (InterruptedException e) {
}
}
top--;
pro = products[top];
return pro;
}
}