public class Producer implements Runnable {
String producerName = null;
StoreHouse storeHouse = null;
public Producer(String producerName, StoreHouse storeHouse) {
this.producerName = producerName;
this.storeHouse = storeHouse;
}
public void setProducerName(String producerName) {
this.producerName = producerName;
}
public String getProducerName() {
return producerName;
}
public void produceProduct() {
int i = 0;
while (true) {
i++;
Product pro = new Product(i);
storeHouse.push(pro);
System.out.println(getProducerName() + " 生产了 " + pro);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
return;
}
}
}
public void run() {
produceProduct();
}
}
消费者与生产者模式2
最新推荐文章于 2022-04-21 16:18:06 发布