BlockingQueue、ArrayBlockingQueue
来源:互联网 发布:淘宝网上开店需要多少钱 编辑:程序博客网 时间:2024/04/30 10:03
jdk从1.5之后加入了BlockingQueue,从字面意思上理解就是阻塞的队列,它是一个接口,所以它就有很多的实现类,现在来看看ArrayBlockingQueue类里面的参数。
/** The queued items */ final Object[] items; /** items index for next take, poll, peek or remove */ int takeIndex; /** items index for next put, offer, or add */ int putIndex; /** Number of elements in the queue */ int count; /* * Concurrency control uses the classic two-condition algorithm * found in any textbook. */ /** Main lock guarding all access */ final ReentrantLock lock; /** Condition for waiting takes */ private final Condition notEmpty; /** Condition for waiting puts */ private final Condition notFull;
items:队列元素对象,由于队列是利用Array实现的,所用就用Object[]数组存取元素
takeIndex:源码注释的很详细,进行take, poll, peek or remove操作时候保存的下表
putIndex:和takeIndex类似,源码注释的很详细
count:数组元素个数
lock:锁,构造法方法的时候初始化锁(不公平锁),具体公平锁、不公平锁相关知识可以查看http://blog.sae.sina.com.cn/archives/5145
notEmpty、notFull:进行takes、puts操作时候的锁
备注:new ArrayBlockingQueue的时候需要传入Queue对应的大小
接下来看个例子
import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;public class TestBlockQueue{public static void main(String[] args) throws Exception{BlockingQueue<Integer> bq = new ArrayBlockingQueue<Integer>(1);bq.put(1);bq.put(1);}}
运行一下会发现程序死锁,原因就是Queue的大小是1,但是往里面塞了2个值,由于存储空间不够,bq等待着队列元素出列后才能插入数据
同样的以下也会发现死锁,因为没有值可以取的出来了
BlockingQueue<Integer> bq = new ArrayBlockingQueue<Integer>(1);bq.put(1);bq.take();bq.take();
总结:ArrayBlockingQueue入列、出列操作时候都会加锁,在多线程情况下保证不会被两个或多个线程取到相同的元素,但由于跟锁有关,所以会产生死锁,使用的时候请多加注意
阅读全文
1 0
- BlockingQueue arrayBlockingqueue
- BlockingQueue、ArrayBlockingQueue
- java BlockingQueue之ArrayBlockingQueue
- SynchronousQueue/BlockingQueue/ArrayBlockingQueue区别
- java.util.concurrent.BlockingQueue ArrayBlockingQueue
- java 并发工具包 BlockingQueue-ArrayBlockingQueue
- 多线程之阻塞队列ArrayBlockingQueue,BlockingQueue
- 《Java源码分析》:BlockingQueue之ArrayBlockingQueue
- 模仿ArrayBlockingQueue编写一个批处理BlockingQueue
- 浅谈JDK BlockingQueue的实现类之ArrayBlockingQueue
- ArrayBlockingQueue
- ArrayBlockingQueue
- ArrayBlockingQueue
- ArrayBlockingQueue
- ArrayBlockingQueue
- ArrayBlockingQueue
- ArrayBlockingQueue
- ArrayBlockingQueue
- 搭建Git服务器的最简单方式
- 用tensorflow的slim模块快速实现mnist手写体识别分类
- Java中throw和throws的区别
- 某S2-052漏洞repetition的一些小白易卡点
- Vue2 构造器的生命周期
- BlockingQueue、ArrayBlockingQueue
- 蓝牙协议分析(4)_IPv6 Over BLE介绍
- 嵌入式开发系统部署
- 为什么需要Secondary Index
- 当OKR遇上SMART就像好马配好鞍
- Linux中对文件描述符的操作
- 常见http状态码
- Error: Could not find or load main class
- ubuntu16.04字体安装