Storage
import java.util.LinkedList;
/**
* 仓库类Storage实现缓冲区
*/
public class Storage
{
private final int MAX_SIZE = 100 ;
private LinkedList<Object> list = new LinkedList<Object>();
public void produce (int num)
{
synchronized (list)
{
while (list.size() + num > MAX_SIZE)
{
System.out.println("【要生产的产品数量】:" + num + "/t【库存量】:"
+ list.size() + "/t暂时不能执行生产任务!" );
try
{
list.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
for (int i = 1 ; i <= num; ++i)
{
list.add(new Object());
}
System.out.println("【已经生产产品数】:" + num + "/t【现仓储量为】:" + list.size());
list.notifyAll();
}
}
public void consume (int num)
{
synchronized (list)
{
while (list.size() < num)
{
System.out.println("【要消费的产品数量】:" + num + "/t【库存量】:"
+ list.size() + "/t暂时不能执行生产任务!" );
try
{
list.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
for (int i = 1 ; i <= num; ++i)
{
list.remove();
}
System.out.println("【已经消费产品数】:" + num + "/t【现仓储量为】:" + list.size());
list.notifyAll();
}
}
public LinkedList<Object> getList ()
{
return list;
}
public void setList (LinkedList<Object> list)
{
this .list = list;
}
public int getMAX_SIZE ()
{
return MAX_SIZE;
}
}
Producer
/**
* 生产者类Producer继承线程类Thread
*/
public class Producer extends Thread
{
private int num;
private Storage storage;
public Producer (Storage storage)
{
this .storage = storage;
}
public void run ()
{
produce(num);
}
public void produce (int num)
{
storage.produce(num);
}
public int getNum ()
{
return num;
}
public void setNum (int num)
{
this .num = num;
}
public Storage getStorage ()
{
return storage;
}
public void setStorage (Storage storage)
{
this .storage = storage;
}
}
Consumer
/**
* 消费者类Consumer继承线程类Thread
*
*/
public class Consumer extends Thread
{
private int num;
private Storage storage;
public Consumer (Storage storage)
{
this .storage = storage;
}
public void run ()
{
consume(num);
}
public void consume (int num)
{
storage.consume(num);
}
public int getNum ()
{
return num;
}
public void setNum (int num)
{
this .num = num;
}
public Storage getStorage ()
{
return storage;
}
public void setStorage (Storage storage)
{
this .storage = storage;
}
}
测试类Test
/**
* 测试类Test
*
*/
public class Test
{
public static void main (String[] args)
{
Storage storage = new Storage();
Producer p1 = new Producer(storage);
Producer p2 = new Producer(storage);
Producer p3 = new Producer(storage);
Producer p4 = new Producer(storage);
Producer p5 = new Producer(storage);
Producer p6 = new Producer(storage);
Producer p7 = new Producer(storage);
Consumer c1 = new Consumer(storage);
Consumer c2 = new Consumer(storage);
Consumer c3 = new Consumer(storage);
p1.setNum(10 );
p2.setNum(10 );
p3.setNum(10 );
p4.setNum(10 );
p5.setNum(10 );
p6.setNum(10 );
p7.setNum(80 );
c1.setNum(50 );
c2.setNum(20 );
c3.setNum(30 );
c1.start();
c2.start();
c3.start();
p1.start();
p2.start();
p3.start();
p4.start();
p5.start();
p6.start();
p7.start();
}
}
输出
【要消费的产品数量】:50 【库存量】:0 暂时不能执行生产任务!
【要消费的产品数量】:30 【库存量】:0 暂时不能执行生产任务!
【要消费的产品数量】:20 【库存量】:0 暂时不能执行生产任务!
【已经生产产品数】:10 【现仓储量为】:10
【要消费的产品数量】:20 【库存量】:10 暂时不能执行生产任务!
【要消费的产品数量】:30 【库存量】:10 暂时不能执行生产任务!
【要消费的产品数量】:50 【库存量】:10 暂时不能执行生产任务!
【已经生产产品数】:10 【现仓储量为】:20
【要消费的产品数量】:50 【库存量】:20 暂时不能执行生产任务!
【要消费的产品数量】:30 【库存量】:20 暂时不能执行生产任务!
【已经消费产品数】:20 【现仓储量为】:0
【已经生产产品数】:10 【现仓储量为】:10
【已经生产产品数】:10 【现仓储量为】:20
【已经生产产品数】:80 【现仓储量为】:100
【要生产的产品数量】:10 【库存量】:100 暂时不能执行生产任务!
【已经消费产品数】:30 【现仓储量为】:70
【已经消费产品数】:50 【现仓储量为】:20
【已经生产产品数】:10 【现仓储量为】:30
【已经生产产品数】:10 【现仓储量为】:40