本文主要是介绍编写一个生产者消费者模式的JAVA工程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
编写一个生产者消费者模式的JAVA工程; 要求:
1)符合生产者消费者模式,避免出现资源访问冲突;
2)输出生产和消费的执行过程;
3)分别统计生产者和消费者的执行时长和等待时长(目前还不知道怎么搞,其他的参考http://blog.csdn.net/monkey_d_meng/article/details/6251879)
- 创建类Storage,作为仓库
import java.util.LinkedList;public class Storage {private final int MAX_SIZE=100;//仓库容量private LinkedList<Object> list = new LinkedList<Object>();//仓库存储的东西/*生产num件货物*/public void produce(int num){ synchronized(list){ //锁定list对象while(list.size()+num>MAX_SIZE){System.out.println("要生产的产品数量:"+num+" 现在的库存:"+list.size()+" 仓库不够放");try {list.wait();//由于生产条件不满足,暂停生产} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("正在生产...");for (int i = 0; i < num; i++) {list.add(new Object());try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println("已经生产的产品:"+num+" 现在的库存为:"+list.size());list.notifyAll();}}/*消费num件东西*/public void consume(int num){synchronized (list) {while (list.size() < num) {System.out.println("要消费的产品数量:"+num+" 现在的库存为:"+list.size()+" 仓库货不够消费");try {list.wait();//由于库存不够,没法消费} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println("正在消费...");for (int i = 0; i < num; i++) {list.remove();try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println("已经消费的产品:"+ num + " 现在的库存为:"+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;}}
- 创建类Producers,负责生产
public class Producers extends Thread{private int num;private Storage storage;public Producers(Storage storage) {this.storage = storage;}/*线程run函数*/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;}}
- 创建类Consumers,负责消费
public class Consumers extends Thread {private int num;private Storage storage;public Consumers(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;}}
- 测试类
/*测试类:用于显示生产者消费者的运行情况*/
public class Test {public static void main(String[] args) {Storage storage = new Storage();//构建一个公用的仓库//创建若干个生产者Producers producer1 = new Producers(storage);Producers producer2 = new Producers(storage);Producers producer3 = new Producers(storage);Producers producer4 = new Producers(storage);Producers producer5 = new Producers(storage);Producers producer6 = new Producers(storage);Producers producer7 = new Producers(storage);//创建若干个消费者Consumers consumer1 = new Consumers(storage);Consumers consumer2 = new Consumers(storage);Consumers consumer3 = new Consumers(storage);/*设置生产者生产数量*/producer1.setNum(10);producer2.setNum(10);producer3.setNum(10);producer4.setNum(10);producer5.setNum(10);producer6.setNum(10);producer7.setNum(80);/*设置消费者生产数量*/consumer1.setNum(50);consumer2.setNum(20);consumer3.setNum(30);/*启动线程*/consumer1.start();consumer2.start();consumer3.start();producer1.start();producer2.start();producer3.start();producer4.start();producer5.start();producer6.start();producer7.start();}
}
运行结果如下(每次结果都不同):
要消费的产品数量:50 现在的库存为:0 仓库货不够消费
要消费的产品数量:20 现在的库存为:0 仓库货不够消费
要消费的产品数量:30 现在的库存为:0 仓库货不够消费
正在生产…
已经生产的产品:10 现在的库存为:10
要消费的产品数量:30 现在的库存为:10 仓库货不够消费
要消费的产品数量:20 现在的库存为:10 仓库货不够消费
要消费的产品数量:50 现在的库存为:10 仓库货不够消费
正在生产…
已经生产的产品:80 现在的库存为:90
正在生产…
已经生产的产品:10 现在的库存为:100
要生产的产品数量:10 现在的库存:100 仓库不够放
要生产的产品数量:10 现在的库存:100 仓库不够放
要生产的产品数量:10 现在的库存:100 仓库不够放
要生产的产品数量:10 现在的库存:100 仓库不够放
正在消费…
已经消费的产品:50 现在的库存为:50
正在消费…
已经消费的产品:20 现在的库存为:30
正在消费…
已经消费的产品:30 现在的库存为:0
正在生产…
已经生产的产品:10 现在的库存为:10
正在生产…
已经生产的产品:10 现在的库存为:20
正在生产…
已经生产的产品:10 现在的库存为:30
正在生产…
已经生产的产品:10 现在的库存为:40
这篇关于编写一个生产者消费者模式的JAVA工程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!