论坛首页 Java企业应用论坛

JAVA模拟 生产者-消费者

浏览 4035 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-10-23   最后修改:2011-06-09
package thread.producerconsumer;

public 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(2000);
			} catch (InterruptedException e) {
				return;
			}
		}
	}

	public void run() {
		produceProduct();
	}

}
以上这个类为生产者
package thread.producerconsumer;

public 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(5000);
			} catch (InterruptedException e) {
				return;
			}
		}
	}

	public void run() {
		consumerProduct();
	}

}
以上为消费者
package thread.producerconsumer;

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);
	}
}
以上为产品类,每个产品具有一个productId 
package thread.producerconsumer;

public class StoreHouse {

	private int base = 0;

	private int top = 0;

	private Product[] products = new Product[10];

	public synchronized void push(Product product) {
		while (top == products.length) {
			notify();
			try {
				System.out.println("仓库已满,正等待消费...");
				wait();
			} catch (InterruptedException e) {
				System.out.println("stop push product because other reasons");
//				必须用while,因为当线程在wait的时候被打断,那么程序会跳出if而去执行下一条语句
			}
		}
		products[top] = product;
		top++;
	}

	public synchronized Product pop() {
		Product pro = null;
		while (top == base) {
			notify();
			try {
				System.out.println("仓库已空,正等待生产...");
				wait();
			} catch (InterruptedException e) {
				System.out.println("stop push product because other reasons");
//				必须用while,因为当线程在wait的时候被打断,那么程序会跳出if而去执行下一条语句
			}
		}
		top--;
		pro = products[top];
		products[top] = null;
		return pro;
	}
}
以上这个类模拟装产品的仓库(以栈为例,先进后出)
package thread.producerconsumer;

public class Test {
	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();
	}
}
以上为测试类 
   发表时间:2008-10-24  
我也贴一个:


package net.myhue.www;

import java.util.concurrent.*;

public class Restaurant {
	ExecutorService execs = Executors.newCachedThreadPool();
	public Beff beff;
	Producer producer = new Producer(this);
	Consumer consumer = new Consumer(this);

	public Restaurant() {
		execs.execute(producer);
		execs.execute(consumer);
	}

	public static void main(String[] args) {
		new Restaurant();
	}
}

// 牛肉
class Beff {
	private final int orderNum;

	Beff(int orderNum) {
		this.orderNum = orderNum;
	}

	@Override
	public String toString() {
		return "Beff " + orderNum;
	}
}

// 生产者
class Producer implements Runnable {
	Restaurant restaurant;
	private int count = 0;

	Producer(Restaurant restaurant) {
		this.restaurant = restaurant;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			while (!Thread.interrupted()) {
				synchronized (this) {
					while (restaurant.beff != null) {
						System.out.println("Can’t Producut,Consumer is eating");
						wait();
					}
				}
				if (count++ == 100) {
					System.out.println("Beff Max Producted!");
					restaurant.execs.shutdownNow();
				}
				synchronized (restaurant.consumer) {
					System.out.println("Produce beff!");
					restaurant.beff = new Beff(count);
					restaurant.consumer.notifyAll();
				}
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			System.out.println("Producer interrupted!!!");
		}
	}
}

// 消费者
class Consumer implements Runnable {
	Restaurant restaurant;

	Consumer(Restaurant restaurant) {
		this.restaurant = restaurant;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			while (!Thread.interrupted()) {
				synchronized (this) {
					while (restaurant.beff == null) {
						System.out.println("Beff isn’t get ready,peasure wait!");
						wait();
					}
				}
				synchronized (restaurant.producer) {
					eat();
					restaurant.producer.notifyAll();
				}
			}
		} catch (InterruptedException e) {
			System.out.println("Consumer interrupted!!");
		}
	}

	private void eat() {
		System.out.println("eating beff!");
		restaurant.beff = null;
	}
}
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics