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入列、出列操作时候都会加锁,在多线程情况下保证不会被两个或多个线程取到相同的元素,但由于跟锁有关,所以会产生死锁,使用的时候请多加注意