ArrayBlockingQueue源码分析(基于jdk1.8)

前言

网上找下ArrayBlockingQueue的源码分析,没找到基于jdk8分析的,索性就自己分析

属性

	// 底层存储元素的数组。final修饰说明一旦初始化,容量不可变,有界的 
    final Object[] items;

    //下一个take, poll, peek or remove操作的index位置
    int takeIndex;

     //下一个put, offer, or add操作的index位置
    int putIndex;

    /** Number of elements in the queue */
    int count;

   
   /**
   *ArrayBlockingQueue并发控制采用的是两个Condition,这也是其性能瓶颈所在
   *put和take都需要获取同一个lock
   */

    //并发控制的锁
    final ReentrantLock lock;

    
    //获取操作等待条件
    private final Condition notEmpty;

   //插入操作等待条件
    private final Condition notFull;

构造方法
ArrayBlockingQueue有提供三个构造方法;

 //带初始大小的阻塞队列,默认非公平
 public ArrayBlockingQueue(int capacity) {
        this(capacity, false);
    }
//带初始大小且是否公平锁的堵塞队列
 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方法,不太常用,
    调用父类的add(E e)方法,父类(AbstractQueue)方法调用的是Queue接口定义的offer(E e)方法,offer(E e)方法由ArrayBlockingQueue实现。
public boolean add(E e) {
       return super.add(e);
   }
public boolean add(E e) {
       if (offer(e))
           return true;
       else
           throw new IllegalStateException("Queue full");
   }
  • offer方法,
    offer方法有两个
    不带超时:队列满时,直接返回false。
    带超时:队列满时,阻塞直到队列可用。
    里面最核心的都用到入队操作enqueue(e),来看下这三个方法
//不带超时offer方法
 public boolean offer(E e) {
        checkNotNull(e);
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            if (count == items.length)//判断队列是不是已满,已满直接返回false
                return false;
            else {
            //未满入队操作,返回true
                enqueue(e);
                return true;
            }
        } finally {
            lock.unlock();
        }
    }

//带超时offer方法
 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;
                 //待用condition类,计算等待时间
                nanos = notFull.awaitNanos(nanos);
            }
            //队列未满入队操作
            enqueue(e);
            return true;
        } finally {
            lock.unlock();
        }
    }
//入队操作
 private void enqueue(E x) {
        // assert lock.getHoldCount() == 1;
        // assert items[putIndex] == null;
        final Object[] items = this.items;
        items[putIndex] = x;
        //如果入队之后队满,将数组下标置为从0开始,防止边界溢出
        if (++putIndex == items.length)
            putIndex = 0;
        //队列元素自增
        count++;
        //通知takes阻塞队列,可以取
        notEmpty.signal();
    }
  • 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();
        }
    }
  • 删除操作
  • poll方法也有两个,带超时不带超时
//不带超时
public E poll() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            return (count == 0) ? null : dequeue();
        } 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;
                nanos = notEmpty.awaitNanos(nanos);
            }
            return dequeue();
        } finally {
            lock.unlock();
        }
    }
//出队
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;
    }
  • take方法
public E take() throws InterruptedException {
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            while (count == 0)
                notEmpty.await();
            return dequeue();
        } finally {
            lock.unlock();
        }
    }
  • peek方法
public E peek() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            return itemAt(takeIndex); 
        } finally {
            lock.unlock();
        }
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值