使用BlockingQueue实现生产者和消费者模式

来源:互联网 发布:1394mac 编辑:程序博客网 时间:2024/04/30 06:00
package com.hjx.product_customer;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;/** * 运行测试类 * @author hjx * */public class MainTest {  public static void main(String[] args) {  //存储所有线程的Map Map<String,Runnable> runnabelMap =new HashMap<String,Runnable>();  //仓库类 Storage storage =new Storage(); //这里使用带缓存功能的线程池 ExecutorService sercice = Executors.newCachedThreadPool();  Productor p1 = null; Customer c1 = null; String name =""; //循环创建各100个生产者线程和消费者线程 for (int i = 0; i < 100; i++) { name =i +"-p"; p1 = new Productor(true,storage,name); runnabelMap.put(name, p1); name =i + "-m"; c1 = new Customer(true,storage,name); runnabelMap.put(name, c1); sercice.submit(p1); sercice.submit(c1);}try {//休眠10秒后TimeUnit.SECONDS.sleep(10); //开始停止所有的线程Set set = runnabelMap.keySet();Iterator it = set.iterator();while (it.hasNext()) {String  keyName = (String ) it.next();BaseRunnable r = (BaseRunnable) runnabelMap.get(keyName);r.setGoing(false); //设置线程停止的标识}//休眠10秒后TimeUnit.SECONDS.sleep(10);//查看仓库里还有多少产品未消费。int count = storage.getQueue().size();System.out.println("仓库里未消费的产品数量: " + count);} catch (InterruptedException e) {e.printStackTrace();} System.out.println("main is end!"); }}

package com.hjx.product_customer;import java.util.concurrent.TimeUnit;/** * 生产者 * @author hjx * */public class Productor extends BaseRunnable {private Storage storage;private String name;public Productor(boolean isGoing,Storage storage,String name) {this.setGoing(isGoing);this.storage = storage;this.name = name;}@Overridepublic void run() {while (this.isGoing()) {String pName ="产品" + (int)(Math.random() * 10000);ProductInfo productInfo =new ProductInfo(pName);try {storage.push(productInfo);System.out.println(name +" 生产产品: "+ productInfo.toString());TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("生产者已经停止生产!");}}

package com.hjx.product_customer;import java.util.concurrent.TimeUnit;/** * 消费者 * @author hjx * */public class Customer extends BaseRunnable {private Storage storage;private String name;public Customer(boolean isGoing,Storage storage,String name) {this.setGoing(isGoing);this.name = name;this.storage = storage;}@Overridepublic void run() {while (this.isGoing()) {try {ProductInfo p = storage.pop();System.out.println(name +" 消费产品: " +p.toString());TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("消费者已经停止消费!");}}

package com.hjx.product_customer;/** * 产品对象 * @author hjx * */public class ProductInfo {private String name;public ProductInfo(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "ProductInfo [name=" + name + "]";}}

package com.hjx.product_customer;/** * 生产者、消费者的父类 * @author hjx * */public class BaseRunnable implements Runnable {private boolean isGoing;//生产者、消费者是否需要继续生产或者消费的条件public boolean isGoing() {return isGoing;}public void setGoing(boolean isGoing) {this.isGoing = isGoing;}@Overridepublic void run() {System.out.println("父类方法");}}


运行效果:





阅读全文
0 0
原创粉丝点击