BlockingQueue、ArrayBlockingQueue

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值