package ConsumerAndProducerProblem;
/**
* @author: zhuwei
* @ClassName: 消费者线程
* @Description: TODO
* @Date: 下午3:57:38
*/
public class Producer implements Runnable
{
//生产的商品数量
private int number;
private Storage storage;
public void produce(int num)
{
try
{
storage.produce(num);
} catch (Exception e)
{
// TODO Auto-generatedcatch block
e.printStackTrace();
}
}
public int getNumber()
{
return number;
}
public void setNumber(int number)
{
this.number = number;
}
public Storage getStorage()
{
return storage;
}
public void setStorage(Storage storage)
{
this.storage = storage;
}
@Override
public void run()
{
// TODO Auto-generatedmethod stub
produce(number);
}
}
创建测试类:
package ConsumerAndProducerProblem;
public class Test
{
public static void main(String[] args)
{
// TODO Auto-generatedmethod stub
//仓库对象
Storage storage = new Storage();
//消费者对象
Consumer c1 = new Consumer();
c1.setNumber(10);
c1.setStorage(storage);
Consumer c2 = new Consumer();
c2.setNumber(80);
c2.setStorage(storage);
//生产者对象
Producer p1 = new Producer();
p1.setNumber(20);
p1.setStorage(storage);
Producer p2 = new Producer();
p2.setNumber(50);
p2.setStorage(storage);
p1.run();
c1.run();
p2.run();
c2.run();
}
}