ArrayBlockQueue

今天我们来看看ArrayBlockQueue 这个有界阻塞队列

public class ArrayBlockingQueue extends AbstractQueue
        implements BlockingQueue, java.io.Serializable {}

从上面我们可以看出 ArrayBlockQueue 是继承了 AbstractQueue 并实现了 BlockingQueue 接口。

ArrayBlockQueue 底层是使用数组实现,并且在初始化时必须指定大小,还有一个默认的 fair 参数, 这个参数的具体使用,后续在进行说明。

ArrayBlockQueue 进行添加元素一共有一下几个方法。

boolean add(E e); 

public boolean add(E e) {
        if (offer(e))
            return true;
        else
            throw new IllegalStateException("Queue full");
    }

我们可以看到具体的add方法是先调用offer方法,如果offer方法返回true,就表示添加成功,返回false,表示队列已满,抛出  IllegalStateException 异常。

boolean offer(E e);

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

第①步:先来检测插入的元素是否为 NULL, 如果为NULL,则抛出 NullPointerException 异常。

第②步:判断当前列队中的元素和数组的大小,如果相等,则表示 队列已满, 直接返回false。

第③步:进行元素的插入操作。

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

这边可以看到 notEmpty.signal(); 这一步操作,表示 唤醒 在等待获取元素的线程,这边顺便说一下,我们的fair,如果为false,表示不公平的获取,如果为 true, 表示为 公平。

boolean offer(E e, long timeout, TimeUnit unit); 表示在有限的等待时间内,如果可以插入到队列则返回 true,否则返回false。

void put(E e) throws InterruptedException(); 

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


首先:我们可以看到,第①步和add方法是相同的,判断元素是否为 null

第②部:调用了 reentrantLock 的可中断异常,表示如果有中断信息,那么立即抛出异常,由上层处理

第③步:和add方法的处理方式不同,当 当前入队数量和队列大小一样时,则进行 等待操作,等有线程从队列中移出元素时,会进行唤醒操作,然后在进行插入(第④步,和add方法一样)。

上面我们对 ArrayBlockQueue 的四种插入方法进行了讲解,下来我们开始讲解 获取的方法。

public E poll() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            return (count == 0) ? null : dequeue(); ①
        } finally {
            lock.unlock();
        }
    }
第①步:我们可以看到,当 队列中没有元素的时候,直接返回 null, 有元素的话,执行 dequeue 方法。
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;
    }
然后获取 队列中的最后一个元素, 符合 FIFO 

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();
        }
    }
这个带有时间的 poll方法,和上面不带时间的几乎差不多, 就是在一定时间内去尝试获取空的队列,如果在这一段时间内,队列中产生了元素,那么就可能获取到元素。(这边主要带了一个可中断异常,在①)

public E take() throws InterruptedException {
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            while (count == 0)
                notEmpty.await(); ①
            return dequeue();
        } finally {
            lock.unlock();
        }
    }
take 方法,和上面的 poll 方法不同,poll方法在队列为空时返回null,而take方法当队列为空时在等待,直到有元素入队时,会进行唤醒操作,这个时候 take 方法就可能会得到执行。(主要就是在 第 ①步的处理上,一个是 return null ,一个是 调用await()方法)

写的第一篇文章,如有错误请多多指正。

你可能感兴趣的:(ArrayBlockQueue)