读书人

java生产者消费者有关问题

发布时间: 2012-12-21 12:03:49 作者: rapoo

java生产者消费者问题

class Producer implements Runnable { ?

?

? ? private String producerName = null;?

?

? ? private 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(400);?

? ? ? ? ? ? } catch (InterruptedException e) {?

? ? ? ? ? ? ? ? return;?

? ? ? ? ? ? }?

? ? ? ? }?

? ? }?

?

? ? public void run() {?

? ? ? ? produceProduct();?

? ? }?

?

}?

?

class Consumer implements Runnable {?

?

? ? private String consumerName = null;?

?

? ? private StoreHouse storeHouse = null;?

?

? ? public Consumer(String consumerName, StoreHouse storeHouse) {?

? ? ? ? this.consumerName = consumerName;?

? ? ? ? this.storeHouse = storeHouse;?

? ? }?

?

? ? public void setConsumerName(String consumerName) {?

? ? ? ? this.consumerName = consumerName;?

? ? }?

?

? ? public String getConsumerName() {?

? ? ? ? return consumerName;?

? ? }?

?

? ? public void consumerProduct() {?

? ? ? ? while (true) {?

? ? ? ? ? ? System.out.println(getConsumerName() + " 消费了 " + storeHouse.pop());?

? ? ? ? ? ? try {?

? ? ? ? ? ? ? ? Thread.sleep(500);?

? ? ? ? ? ? } catch (InterruptedException e) {?

? ? ? ? ? ? ? ? return;?

? ? ? ? ? ? }?

? ? ? ? }?

? ? }?

?

? ? public void run() {?

? ? ? ? consumerProduct();?

? ? }?

?

}?

?

?

?

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];?

?

? ? public synchronized void push(Product product) {?

?

? ? ? ? if (top == products.length) {?

?

? ? ? ? ? ? try {?

? ? ? ? ? ? ? ? System.out.println("仓库已满,正等待消费...");?

? ? ? ? ? ? ? ? wait();

? ? ? ? ? ? } catch (InterruptedException e) {?

? ? ? ? ? ? ? ? System.out.println("stop push product because other reasons");?

?

? ? ? ? ? ? }?

? ? ? ? }?

? ? ? ? products[top] = product;?

? ? ? ? top++;?

? ? notify();?

? ? }?

?

? ? public synchronized Product pop() {?

? ? ? ? Product pro = null;?

?

? ? ? ? if (top == base) {?

?

? ? ? ? ? ? try {?

? ? ? ? ? ? ? ? System.out.println("仓库已空,正等待生产...");?

? ? ? ? ? ? ? ? wait();?

? ? ? ? ? ? } catch (InterruptedException e) {?

? ? ? ? ? ? ? ? System.out.println("stop push product because other reasons");?

?

? ? ? ? ? ? }?

? ? ? ? }?

? ? ? ? top--;?

? ? ? ? pro = products[top];?

? ? ? ? products[top] = null;?

? ? ? ? notify();?

? ? ? ? return pro;?

? ? }?

}?

?

public class TestPC {?

? ? public static void main(String[] args) {?

? ? ? ? StoreHouse storeHouse = new StoreHouse();?

? ? ? ? Producer producer = new Producer("生产者", storeHouse);?

? ? ? ? Consumer comsumer = new Consumer("消费者", storeHouse);?

? ? ? ? Thread t1 = new Thread(producer);?

? ? ? ? Thread t2 = new Thread(comsumer);?

? ? ? ? t1.start();?

? ? ? ? t2.start();?

? ? }?

}?

读书人网 >编程

热点推荐