ArrayBlockingQueue源码解析

ArrayBlockingQueue源码解析

BlockingQueue是阻塞队列,首先是一个循环队列,支持入队出队操作;同时可以在队列满时阻塞入队,在队列空时阻塞出队。(类似一种消费者生产者模式)

ArrayBlockingQueue显然是通过数组来实现阻塞队列。

那么它是如何通过数组来实现队列呢?又如何实现阻塞入队出队呢?同时它又是线程安全的类,它是如何保证在多线程的情况下依然保证入队出队的正确呢?

一、定义

public class ArrayBlockingQueue<E> extends AbstractQueue<E>
        implements BlockingQueue<E>, java.io.Serializable

二、字段

 	/** 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;

    /**
     * Shared state for currently active iterators, or null if there
     * are known not to be any.  Allows queue operations to update
     * iterator state.
     */
    transient Itrs itrs = null;

三、构造函数


    /**
     * 构造一个给定容量(固定不变)的阻塞队列,默认使用非公平锁
     */
    public ArrayBlockingQueue(int capacity) {
        this(capacity, false);
    }

    /**
     * 构造一个给定容量(固定不变)的阻塞队列,使用公平锁或非公平锁(根据入参fair决定)
     * 公平锁是以FIFO的顺序获取锁,非公平锁以不确定的顺序获取
     */
    public ArrayBlockingQueue(int capacity, boolean fair) {
        if (capacity <= 0)
            throw new IllegalArgumentException();
        this.items = new Object[capacity];
        lock = new ReentrantLock(fair);
        notEmpty = lock.newCondition();
        notFull =  lock.newCondition();
    }

    /**
     * 构造一个阻塞队列,并且把集合中元素放入其中
     */
    public ArrayBlockingQueue(int capacity, boolean fair,
                              Collection<? extends E> c) {
        this(capacity, fair);

        final ReentrantLock lock = this.lock;
        lock.lock(); // Lock only for visibility, not mutual exclusion
        try {
            int i = 0;
            try {
                for (E e : c) {
                		//每一个位置都必须非空
                    checkNotNull(e);
                    items[i++] = e;
                }
            } catch (ArrayIndexOutOfBoundsException ex) {
                throw new IllegalArgumentException();
            }
            count = i;
            //移动指针
            putIndex = (i == capacity) ? 0 : i;
        } finally {
            lock.unlock();
        }
    }

四、核心方法 放入元素(入队)

add、offer、put

  • add、offer都是立即返回的,如果有空位置就入队,返回true,满了就直接返回false。
  • put是阻塞放入,如果有空位置就直接入队;如果队列满了,就阻塞等待,直到有空位置再进行入队。
    public boolean add(E e) {
        if (offer(e))
            return true;
        else
            throw new IllegalStateException("Queue full");
    }
    
    public boolean offer(E e) {
        checkNotNull(e);
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            if (count == items.length)
                return false;
            else {
                enqueue(e);
                return true;
            }
        } finally {
            lock.unlock();
        }
    }
    
     /**
     * Inserts the specified element at the tail of this queue, waiting
     * for space to become available if the queue is full.
     *
     * @throws InterruptedException {@inheritDoc}
     * @throws NullPointerException {@inheritDoc}
     */
    public void put(E e) throws InterruptedException {
        checkNotNull(e);
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
        	//循环判断,如果当前队列已满,就进行等待,等待别的线程唤醒
            while (count == items.length)
                notFull.await();
            enqueue(e);
        } finally {
            lock.unlock();
        }
    }

add、offer、put 都调用enqueue方法。

   /**
     * Inserts element at current put position, advances, and signals.
     * Call only when holding lock.只有持有锁才能调用该方法
     * 进行入队操作,并移动putIndex指针,计数加一,唤醒一个出队线程。
     */
    private void enqueue(E x) {
        // assert lock.getHoldCount() == 1;
        // assert items[putIndex] == null;
        final Object[] items = this.items;
        items[putIndex] = x;
        if (++putIndex == items.length)
            putIndex = 0;
        count++;
        notEmpty.signal();
    }

五、核心方法 取出元素(出队)

poll、take

  • poll方法立即返回,队列空立即返回null,不空取出一个元素返回。
  • take方法可能阻塞,队列空阻塞等待,直到有数据放入,再取出一个返回。不为空直接取出一个返回。
   public E poll() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            return (count == 0) ? null : dequeue();
        } finally {
            lock.unlock();
        }
    }

    public E take() throws InterruptedException {
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
        	//当数组为空时,进行等待
            while (count == 0)
                notEmpty.await();
            return dequeue();
        } finally {
            lock.unlock();
        }
    }
    
    //poll和take都会调用dequeue,进行出队
     /**
     * Extracts element at current take position, advances, and signals.
     * Call only when holding lock.只有持有锁时才能调用
     * 取出元素,修改takeIndex指针,计数减一,唤醒入队等待线程。
     */
    private E dequeue() {
        // assert lock.getHoldCount() == 1;
        // assert items[takeIndex] != null;
        final Object[] items = this.items;
        @SuppressWarnings("unchecked")
        E x = (E) items[takeIndex];
        //将该位置置为空
        items[takeIndex] = null;
        if (++takeIndex == items.length)
            takeIndex = 0;
        count--;
        if (itrs != null)
            itrs.elementDequeued();
        notFull.signal();
        return x;
    }

六、其他方法

	public E peek() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            return itemAt(takeIndex); // null when queue is empty
        } finally {
            lock.unlock();
        }
    }
    
      public int size() {
      	//由于count字段没有使用volatile,所以也需要加锁获取值。
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            return count;
        } finally {
            lock.unlock();
        }
    }
    
     //限时等待进行入队
     public boolean offer(E e, long timeout, TimeUnit unit)
        throws InterruptedException {

        checkNotNull(e);
        long nanos = unit.toNanos(timeout);
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            while (count == items.length) {
                if (nanos <= 0)
                    return false;
                //只等待相应的时间,如果时间过后队列还是满着,就返回false
                nanos = notFull.awaitNanos(nanos);
            }
            enqueue(e);
            return true;
        } finally {
            lock.unlock();
        }
    }
    
     //限时等待 出队
     public E poll(long timeout, TimeUnit unit) throws InterruptedException {
        long nanos = unit.toNanos(timeout);
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            while (count == 0) {
                if (nanos <= 0)
                    return null;
                //只等待相应的时间,如果时间过后队列还是空着,就返回false
                nanos = notEmpty.awaitNanos(nanos);
            }
            return dequeue();
        } finally {
            lock.unlock();
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值