阻塞队列BlockingQueue
来源:互联网 发布:澳门娱乐网络博客 编辑:程序博客网 时间:2024/05/21 14:47
BlockingQueue最终会有四种状况,抛出异常、返回特殊值、阻塞、超时,下表总结了这些方法:
抛出异常特殊值阻塞超时插入add(e)
offer(e)
put(e)
offer(e, time, unit)
移除remove()
poll()
take()
poll(time, unit)
检查element()
peek()
不可用不可用
BlockingQueue是个接口,有如下实现类:
1. ArrayBlockQueue:一个由数组支持的有界阻塞队列。此队列按 FIFO(先进先出)原则对元素进行排序。创建其对象必须明确大小,像数组一样。
2. LinkedBlockQueue:一个可改变大小的阻塞队列。此队列按 FIFO(先进先出)原则对元素进行排序。创建其对象如果没有明确大小,默认值是Integer.MAX_VALUE。链接队列的吞吐量通常要高于基于数组的队列,但是在大多数并发应用程序中,其可预知的性能要低。
3. PriorityBlockingQueue:类似于LinkedBlockingQueue,但其所含对象的排序不是FIFO,而是依据对象的自然排序顺序或者是构造函数所带的Comparator决定的顺序。
4. SynchronousQueue:同步队列。同步队列没有任何容量,每个插入必须等待另一个线程移除,反之亦然。
下面使用ArrayBlockQueue来实现之前实现过的生产者消/费者模式,代码如下:
执行结果: 从结果看,启动10个放鸡蛋线程和10个取鸡蛋线程,前5个放入鸡蛋的线程成功执行,到第6个,发现盘子满了,阻塞住,这时切换到取鸡蛋线程执行,成功实现了生产者/消费者模式。java.util.concurrent包是个强大的包! 0 0
- 阻塞队列(BlockingQueue)
- 阻塞队列BlockingQueue
- 阻塞队列 BlockingQueue
- BlockingQueue: 阻塞队列 接口
- 阻塞队列BlockingQueue
- 阻塞队列(BlockingQueue)
- 阻塞队列BlockingQueue使用方法
- 阻塞队列 BlockingQueue LinkedBlockingQueue
- Java 阻塞队列 BlockingQueue
- java 阻塞队列BlockingQueue
- 阻塞队列(BlockingQueue)
- 阻塞队列BlockingQueue
- 阻塞队列BlockingQueue
- 阻塞队列BlockingQueue
- 阻塞队列BlockingQueue
- 阻塞队列BlockingQueue
- 阻塞队列BlockingQueue用法
- Java : BlockingQueue 阻塞队列
- 需求从0到1
- Hadoop,master和slave简单的搭建
- 外观模式
- Blue Jeans
- Ultra-QuickSort
- 阻塞队列BlockingQueue
- LeetCode 290. Word Pattern
- 高精度加法
- leetcode(83).350. Intersection of Two Arrays II
- NFS 网络文件系统 安装配置
- poj2942 Knights of the Round Table
- Android热修复原理(HotFix)初涉
- hdu 2612 Find a way
- 递归