ArrayBlockQueue源码解析

366 阅读3分钟

在线程池中有四种阻塞队列,今天看下其中的一种ArrayBlockQueue,它是一种基于数组的有限的阻塞队列,而且是FIFO队列。这个类是在JUC包下,所以还是线程安全的。

ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(10);

这是初始化操作,点进去看下它的构造函数

如果初始给定队列大小的值小于0,直接抛出异常

首选初始化数组,数组大小一旦定下来就不能改变,所以说是有限的阻塞队列

因为ArrayBlockQueue是基于AQS来实现同步安全的,所以参数 fair 代表的使用公平锁还是非公平锁

最后二行代码是为了更加细粒化锁的控制

接下来看下入队操作:

add():

如果队列满了那么就抛出异常。看下offer(e)这个方法

 public boolean offer(E e) {
        //检查值是否为空,如果为空,抛出空指针异常
        checkNotNull(e);
        //当前线程尝试获取锁
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            //如果队列满了,返回 false,那么add方法直接抛出异常
            if (count == items.length)
                return false;
            else {
                //队列没满,插入元素
                enqueue(e);
                return true;
            }
        } finally {
            //释放锁
            lock.unlock();
        }
    }

来看下 enqueue()方法

 private void enqueue(E x) {
        //获取当前的数组
        final Object[] items = this.items;
        //将元素放到队列末尾
        items[putIndex] = x;
        if (++putIndex == items.length)
            putIndex = 0;
        count++;
        //唤醒其它获取队列元素的线程
        notEmpty.signal();
    }

offer()方法就是上面分析的那样,只是当队列满了直接返回false。

offer(E,Time,TimeUnit):

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) {
            //如果在指定时间还没有空余空间,则返回false
            if (nanos <= 0)
                return false;
            nanos = notFull.awaitNanos(nanos);
        }
        enqueue(e);
        return true;
    } finally {
        lock.unlock();
    }
}

put():

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();
        }
    }

接下来看下获取元素的方法

take():

public E take() throws InterruptedException {
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            while (count == 0)
                //如果队列没有元素,则会一直阻塞,直到队列有元素为止
                notEmpty.await();
            return dequeue();
        } finally {
            lock.unlock();
        }
    }

poll():

public E poll() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            //如果队列为空,那么返回 null,如果有元素就获取队列第一个元素
            return (count == 0) ? null : dequeue();
        } finally {
            lock.unlock();
        }
    }
    
    private E dequeue() {
        final Object[] items = this.items;
        @SuppressWarnings("unchecked")
        //获取元素,并将该位置的值设置为null
        E x = (E) items[takeIndex];
        items[takeIndex] = null;
        if (++takeIndex == items.length)
            takeIndex = 0;
        count--;
        if (itrs != null)
            itrs.elementDequeued();
        //唤醒其它插入元素的线程
        notFull.signal();
        return x;
}

poll(E,Time,TimeUnit): 与poll()不一样的是,它会等待一段时间,如果这段时间之后队列还是没有元素,那么再返回 null

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;
                nanos = notEmpty.awaitNanos(nanos);
            }
            return dequeue();
        } finally {
            lock.unlock();
        }
    }

remove(): 它相当于是执行了poll()方法

public E remove() {
        E x = poll();
        //如果获取到了元素那么直接返回,如果没有获得到元素,或者是null,那么直接抛出异常
        if (x != null)
            return x;
        else
            throw new NoSuchElementException();
    }

今天就讲下ArrayBlockQueue的插入和获取的方法,现在能够理解为什么ArrayBlockQueue是一个基于数组的有限的安全的队列了吧