生产者消费者模型实现<二>真实实现BlockingQueue

为什么叫真实实现呢?上文的模拟实现中,生成或者消费都是在命令行打印了一个提示,而真实的实现肯定是生产或者消费Object的,而不仅仅是在Concole打印一行字符而已,下面借助ArrayBlockingQueue实现(不熟悉ArrayBlockingQueue的可以去看并发的书籍,如《java 并发编程实战》,《java 并发编程的艺术》等)。


Store.java

package test.producerAndConsumer2;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * 仓库类
 * create by qiuping.wu on 2015-08-12
 */
public class Store<E> {
	private BlockingQueue<E> bq;

	//最大容量呢
	public Store(int max_cap) {
		super();
		bq = new ArrayBlockingQueue<E>(max_cap);
	}

	public void Produce(E product) {
		try {
			bq.put(product);
			System.out.println(Thread.currentThread().getName()+"生产者往仓库放入+++++++++++++++商品:"+product.toString()+",仓库目前大小:" +bq.size());
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public E Consume() {
		try {
			E e = bq.take();
			System.out.println(Thread.currentThread().getName()+"消费者从仓库取出---------------商品:"+e.toString()+",仓库目前大小:" +bq.size());
			return e;
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
		Store store  = new Store<String>(10);
		store.Produce(1);
		store.Produce(2);
		store.Consume();
		store.Consume();
		store.Consume();
	}

}




Producer.java

package test.producerAndConsumer2;

/**
 * 生产者类
 *  * create by qiuping.wu on 2015-08-12
 */
public class Producer implements Runnable {
private Store store;
private AtomicInteger count = new AtomicInteger(0);//不能用private int count=0;
	public Producer(Store store) {
		this.store = store;
	}
	
	@Override
	public void run()
	{
		while(true)
		{
			store.Produce(count.addAndGet(1));//不能用count++
			
			try {
				Thread.sleep(900);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}




Consumer.java

package test.producerAndConsumer2;

/**
 * 消费者类
 *  * create by qiuping.wu on 2015-08-12
 */
public class Consumer implements Runnable {
	private Store store;

	public Consumer(Store store) {
		this.store = store;
	}

	@Override
	public void run() {
		while (true) {
			store.Consume();

			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}


测试一:生产者:消费者= 1:1


testSingle.java

package test.producerAndConsumer2;

/**
 * 单生产者单消费者测试类
 *  * create by qiuping.wu on 2015-08-12
 */
public class testSingle {

	public static void main(String[] args) {
		Store store  = new Store(10);
		Producer p = new Producer(store);
		Consumer c  = new Consumer(store);
		new Thread(p).start();
		new Thread(c).start();
	}

}



测试二:生产者:消费者= m:n

testMulti.java

package test.producerAndConsumer2;

/**
 * 多生产者多消费者测试类
 *  * create by qiuping.wu on 2015-08-12
 */
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class testMulti {

	public static void main(String[] args) {
		ExecutorService es = Executors.newCachedThreadPool();

		Store store = new Store<String>(20);
		Producer p = new Producer(store);
		Consumer c = new Consumer(store);

		for (int i = 0; i < 3; i++) {
			es.submit(p);
		}
		for (int i = 0; i < 4; i++) {
			es.submit(c);
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值