生产者和消费者
//产品
class Product
{
private String productName;
public Product(String name)
{
this.productName = name;
}
public String getProductName()
{
return this.productName.toString();
}
}
// 生产者
class Producer extends Thread
{
private Warehouse warehouse;
private static int produceName = 0;
private boolean runing = false;
public Producer(Warehouse warehouse, String name)
{
// setName(name);
super(name);
this.warehouse = warehouse;
}
public void start()
{
this.runing = true;
super.start();
}
public void run()
{
Product product;
try
{
while (runing)
{
produceName++;
product = new Product(produceName + "");
// System.out.println("商品的名称是"+this.produceName);
this.warehouse.storageProduct(product);
sleep(300);
}
} catch (InterruptedException e)
{
// TODO: handle exception
e.printStackTrace();
}
}
public void stopProducer()
{
synchronized (warehouse)
{
System.out.println(currentThread().getName() + "已经停止生产");
this.runing = false;
warehouse.notifyAll();
}
}
// 判断此线程是否在运行
public boolean isRuning()
{
return this.runing;
}
}
// 消费者
class Consumer extends Thread
{
private Warehouse warehouse;
private boolean runing = false;
public Consumer(Warehouse warehouse, String name)
{
super(name);
this.warehouse = warehouse;
}
public void start()
{
this.runing = true;
super.start();
}
public void run()
{
Product product;
try
{
while (runing)
{
product = warehouse.getProduct();
sleep(500);
}
} catch (InterruptedException e)
{
// TODO: handle exception
e.printStackTrace();
}
}
public void stopConsumer()
{
synchronized (warehouse)// 这里不明白
{
this.runing = false;
warehouse.notifyAll();
}
}
public boolean isRuning()
{
return this.runing;
}
}
class Warehouse
{
private int CAPACITY = 15;
private Product[] products;
private int front = 0;
public Warehouse()
{
this.products = new Product[CAPACITY];
}
public Warehouse(int capacity)
{
this();
if (capacity > 0)
{
CAPACITY = capacity + 1;
products = new Product[CAPACITY];
}
}
public Product getProduct() throws InterruptedException
{
synchronized (this)
{
boolean ConsumerRuning = true;
Thread currentThread = Thread.currentThread();
if (currentThread instanceof Consumer)// instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。
{
ConsumerRuning = ((Consumer) currentThread).isRuning();// 得到消费者线程的状态
} else
return null;// 非消费者不能获得产品
while (front == 0 && ConsumerRuning)// 如果仓库中没有商品并且消费者线程还在运行 就让消费者线程等待
{
wait();// 让当前的线程处于等待状态 即消费者线程
ConsumerRuning = ((Consumer) currentThread).isRuning();
}
if (!ConsumerRuning)// 如果消费者线程没有运行了(即处于休眠中) 那么就取消获得产品
return null;
Product product = products[front];// 消费者获得了一个产品
System.out.println("消费者" + currentThread.getName() + "获得的商品" + product.getProductName());
front--;
// 通知其他等待线程
notify();//唤醒其他等待的线程
return product;
}
}
public void storageProduct(Product product) throws InterruptedException
{
synchronized (this)
{
boolean producerRuning = true;// 标志生产者的线程是否在运行
Thread currentThread = Thread.currentThread();// 获得当前正在运行的线程
if (currentThread instanceof Producer)
{
producerRuning = ((Producer) currentThread).isRuning();
} else
return;
while ((front == CAPACITY) && producerRuning)// 仓库满了 并且生产者线程还在运行
{
wait();// 就让生产者线程等待
producerRuning = ((Producer) currentThread).isRuning();
}
if (!producerRuning)// 如果生产者线程已经停止运行了 不在进行储存
return;
front++;
products[front] = product;
System.out.println(currentThread.getName() + "生产的" + product.getProductName()
+ "已经存入仓库\n" + "当前仓库的商品量是" + front);
notify();// 唤醒其他等待的线程 因为此时消费者线程可能在等待中
}
}
}
public class TestProduct
{
public static void main(String[] args)
{
// TODO 自动生成的方法存根
Warehouse warehouse = new Warehouse(10);
Producer producer1 = new Producer(warehouse, "小米");
Producer producer2 = new Producer(warehouse, "苹果");
Producer producer3 = new Producer(warehouse, "华为");
Consumer consumer1 = new Consumer(warehouse, "崔梦梦");
Consumer consumer2 = new Consumer(warehouse, "江浩");
Consumer consumer3 = new Consumer(warehouse, "邓超");
Consumer consumer4 = new Consumer(warehouse, "刘首哲");
Consumer consumer5 = new Consumer(warehouse, "朱帅");
Consumer consumer6 = new Consumer(warehouse, "付阳");
producer1.start();
producer2.start();
producer3.start();
consumer1.start();
consumer2.start();
consumer3.start();
consumer4.start();
consumer5.start();
consumer6.start();
try
{
Thread.sleep(2000);//不让线程那么快结束 方便看运行结果
} catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
producer1.stopProducer();
producer2.stopProducer();
producer3.stopProducer();
consumer1.stopConsumer();
consumer2.stopConsumer();
consumer3.stopConsumer();
consumer4.stopConsumer();
consumer5.stopConsumer();
consumer6.stopConsumer();
System.out.print("hahha");
}
}
梦里不知身是客,一晌贪欢。