package cn.wtu.executor; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; /** **版本2... * 使用新版本的并发库改写生产者消费者 * 可以支持取消的版本 * @author JZY 2010-12-7 */ public class ProducerAndConsumer1 { static ExecutorService executorService = Executors.newFixedThreadPool(4); //ExecutorService增强了Executor接口,加入了一些生命周期(liife cycle)的支持 //让线程的关闭更容易 public static void main(String[] args) { BlockingQueue<Food> container= new LinkedBlockingQueue<Food>(); Producer producer = new Producer(container); Consumer consumer = new Consumer(container); Future producerFuture = executorService.submit(producer);//封装成future对象 //增强了一些方法 Future consumerFuture = executorService.submit(consumer); while(!producerFuture.isDone()||!consumerFuture.isDone()) { } //完成任务后 关闭线程池 executorService.shutdown(); System.out.println("退出程序......."); } }
这些日在在学校无所事事,突然发现了一本好书<<Java并发编程实践>>,作者都是牛人啊...Joshua Bloch jdk CollectionFrameWork缔造者....DougLea教授,jdk并发库的缔造者...Brian Goetz IBM Java社区的大牛..等等
额。。这本书真的很好,虽然我现在的水平很多看不懂,理解的不深刻,但一定会努力的学习..不断温故,相信以后工作在大量的时间后会慢慢有所体会..
先发个自己的入门。。。
package cn.wtu.executor; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; /** * Doug Lea 教授...Joshua Bloch大神 Brian Goetz 哥.. * 使用新版本的并发库改写生产者消费者 * Executor框架的提出 解耦了任务的提出与任务的执行 * FixedThreadPool内部也内置了一个BlockingQueue private final BlockingQueue<Runnable> * workQueue; 用于存储提交给她的任务 * * @author JZY 2010-12-7 */ public class ProducerAndConsumer { static Executor executor = Executors.newFixedThreadPool(4); public static void main(String[] args) { BlockingQueue<Food> container = new LinkedBlockingQueue<Food>(); Producer producer = new Producer(container); Consumer consumer = new Consumer(container); executor.execute(producer); executor.execute(consumer); } } class Producer implements Runnable { // 生产的food将放入这个容器中 // blockingqueue自己实现了阻塞当你调用get时如果容器为空为阻塞,这样我们就不必在客户端 // 自己控制 BlockingQueue<Food> container; public Producer(BlockingQueue<Food> container) { this.container = container; } @Override public void run() { for (int i = 0; i < 100; i++) { Food food = produceFood(i); // container.add(food); //额 ...add方法是不支持阻塞的... try { container.put(food); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // System.out.println("生产"+food); 放在这里就是bug } } private Food produceFood(int i) { Food food = new Food(i + 1, "食物" + (i + 1)); System.out.println("生产" + food);// 放在这里是不合适的,有提前 // 最好放在put方法的内部 真正的加入容器之后 return food; } } class Consumer implements Runnable { BlockingQueue<Food> container; public Consumer(BlockingQueue<Food> container) { this.container = container; } @Override public void run() { for (int i = 0; i < 100; i++) { consumeFood(); } } private void consumeFood() { Food food; try { food = container.take();// Retrieves and removes the head of this // queue, // waiting if necessary until an element becomes available System.out.println("消费" + food);// 会存在延迟 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class Food { Integer id; String desccibtion; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Override public String toString() { return "Food [id=" + id + ", desccibtion=" + desccibtion + "]"; } public Food(Integer id, String desccibtion) { this.id = id; this.desccibtion = desccibtion; } public String getDesccibtion() { return desccibtion; } public void setDesccibtion(String desccibtion) { this.desccibtion = desccibtion; } }