第一步,定义一个商品类。
package com.moon.thread;
/**
*
* @author cenyuan
* 商品类
*/
public class Product {
private String name;
private String id;
public Product()
{
}
public Product(String name)
{
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
第二步,定义仓库类
package com.moon.thread;
/**
*
* @author cenyuan
* 仓库类
*/
public class Storage
{
private String name ;//仓库名字
private String id; //仓库编号
private static final int MAX_STORAGE_NUM = 10;//仓库的最大容量
private static final int MIN_STORAGE_NUM = 1 ;//仓库的最小容量
private Product[] products;//商品集
private int size=0; //商品集大小
public Storage()
{
products = new Product[MAX_STORAGE_NUM];
}
public synchronized Product put(Product prod)
{
//System.out.println("MAX_STORAGE_NUM < size="+(MAX_STORAGE_NUM < size));
if(MAX_STORAGE_NUM < size)
{
try
{
System.out.println(Thread.currentThread()+"等待消费者消费!");
this.wait();//如果商品集已经超过仓库的容量则生产者线程等待
} catch (InterruptedException e)
{
System.out.println("当生产产品" + prod.getName()+"出现错误");
e.printStackTrace();
}
}
else
{
System.out.println("开始生产" + prod.getName());
products[size] = prod;
size++;
System.out.println("仓库里面有 = " +size + "产品");
this.notifyAll();
}
return prod;
}
public synchronized Product get()
{
Product prod = null;
//System.out.println("MIN_STORAGE_NUM > size : " + MIN_STORAGE_NUM +">" +size);
if(MIN_STORAGE_NUM > size)
{
try
{
System.out.println(Thread.currentThread()+"等待生产者生产!");
this.wait();//如果商品集已经小于仓库的最小容量则消费者线程等待
} catch (InterruptedException e)
{
System.out.println("当消费产品时出现错误");
e.printStackTrace();
}
}
else
{
size--;
prod = products[size];
//System.out.println("开始消费" + prod.getName());
System.out.println(Thread.currentThread()+"已消费,现在仓库里面有 = " +size + "产品");
this.notifyAll();
}
return prod;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
第三步,定义生产者类
package com.moon.thread;
/**
*
* @author cenyuan
* 生产者类
*/
public class Producter implements Runnable{
private String name;
private Storage storage;
public void create() throws InterruptedException
{
for(int i=0;;i++)
{
Thread.sleep(800);
// System.out.println("Producter Current Thread : " + Thread.currentThread());
Product prod = new Product("Book "+i);
// System.out.println(name + "准备生产产品:"+prod.getName());
storage.put(prod);
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Storage getStorage() {
return storage;
}
public void setStorage(Storage storage) {
this.storage = storage;
}
public Producter(String name,Storage storage)
{
this.storage = storage;
this.name = name;
}
@Override
public void run() {
try {
create();
} catch (InterruptedException e) {
System.out.println("生产时出现错误!");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
第四步,定义一个消费者类
package com.moon.thread;
/**
*
* @author cenyuan
* 消费者类
*/
public class Customer implements Runnable{
private String name;
private Storage storage;
public void cust() throws InterruptedException
{
while(true)
{
Thread.sleep(500);
//System.out.println("Customer Current Thread : " + Thread.currentThread());
//System.out.println(name + "准备消费产品:");
Product prod = storage.get();
if(prod!=null)
System.out.println(name + "消费产品:"+prod.getName()+" 结束");
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Storage getStorage() {
return storage;
}
public void setStorage(Storage storage) {
this.storage = storage;
}
public Customer(String name,Storage storage)
{
this.name = name;
this.storage = storage;
}
@Override
public void run() {
try {
cust();
} catch (InterruptedException e) {
System.out.println("消费时出现错误!");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
第五步 定义一个测试类
package com.moon.thread;
public class Tester {
/**
* @param args
*/
public static void main(String[] args) {
Storage storage = new Storage();
Producter proder1 = new Producter("王杰",storage);
Producter proder2 = new Producter("蒋东",storage);
Customer cu1 = new Customer("胡株",storage);
Customer cu2 = new Customer("何德",storage);
Customer cu3 = new Customer("张三",storage);
Thread thread1 = new Thread(proder1,"王杰");
Thread thread2 = new Thread(cu3,"张三");
Thread thread3 = new Thread(cu1,"胡株");
Thread thread4 = new Thread(cu2,"何德");
thread3.start();
thread1.start();
thread2.start();
thread4.start();
System.out.println("Main thread end !");
}
}