为什么叫真实实现呢?上文的模拟实现中,生成或者消费都是在命令行打印了一个提示,而真实的实现肯定是生产或者消费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();
}
}
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();
}
}
}
}
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);
}
}
}