生產者消費者模型實現真實實現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);
		}
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章