标签:
ArrayBlockingQueue, DelayQueue, LinkedBlockingDeque, LinkedBlockingQueue, PriorityBlockingQueue, SynchronousQueue
下面是代码例子
/** * */ package concurrent; import java.util.Random; import java.util.concurrent.BlockingQueue; /** * @author think * */ class Producer implements Runnable { private final BlockingQueue<Integer> queue; Producer(BlockingQueue<Integer> q) { queue = q; } public void run() { try { while (true) { queue.put(produce()); } } catch (InterruptedException ex) { System.out.println(ex); } } Integer produce() { Random random = new Random(); return random.nextInt(); } }
/** * */ package concurrent; import java.util.concurrent.BlockingQueue; /** * @author think * */ class Consumer implements Runnable { private final BlockingQueue<Integer> queue; Consumer(BlockingQueue<Integer> q) { queue = q; } public void run() { try { while (true) { consume(queue.take()); } } catch (InterruptedException ex) { System.out.println(ex); } } void consume(Integer x) { System.out.println(x); } }
/** * */ package concurrent; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; /** * @author think * */ class Setup { public static void main(String[] args) { BlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(100); Producer p = new Producer(q); Consumer c1 = new Consumer(q); Consumer c2 = new Consumer(q); new Thread(p).start(); new Thread(c1).start(); new Thread(c2).start(); } }
PriorityQueue
相同的顺序规则,并且提供了阻塞获取操作。
http://lavasoft.blog.51cto.com/62575/222530/
http://www.cnblogs.com/jackyuj/archive/2010/11/24/1886553.html
http://vakinge.iteye.com/blog/625613
http://blog.csdn.net/xin_jmail/article/details/26157971
http://zzhonghe.iteye.com/blog/826757
标签:
原文地址:http://www.cnblogs.com/usual2013blog/p/4231476.html