JavaSE(25)——阻塞队列

阻塞队列

操作抛出异常有返回值,不抛异常阻塞等待超时等待
插入addofferputoffer()
删除removepolltakepoll()
获取elementpeek

ArrayBlockingQueue

  • 属性

    	//队列元素
        final Object[] items;
    	//下一个元素的下标(被take,poll,peek,remove)
        int takeIndex;
    	//下一个元素的下标(被put,offer,add)
        int putIndex;
    	//队列中的元素数
        int count;
    	//重入锁
        final ReentrantLock lock;
    	//队列不为空条件
        private final Condition notEmpty;
    	//队列不满条件
        private final Condition notFull;
    	
        transient Itrs itrs = null;
    
  • add()

    public boolean add(E e) {
        return super.add(e);
    }
    
    //BlockQueue 中的方法
    public boolean add(E e) {
        if (offer(e))
            return true;
        else
            throw new IllegalStateException("Queue full");
    }
    
    
    public boolean offer(E 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();
        }
    }
    
  • 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();
        }
    }
    
  • enqueue()

    private void enqueue(E x) {
        final Object[] items = this.items;
        items[putIndex] = x;
        //入队后,下标+1,(循环队列)
        if (++putIndex == items.length)
            putIndex = 0;
        count++;
        //唤醒notEmpty
        notEmpty.signal();
    }
    
  • 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 {
            return (count == 0) ? null : dequeue();
        } finally {
            lock.unlock();
        }
    }
    
  • dequeue()

    private E dequeue() {
        final Object[] items = this.items;
        //获取需要出队的元素
        E x = (E) items[takeIndex];
        items[takeIndex] = null;
        //出队后下标+1,(循环队列)
        if (++takeIndex == items.length)
            takeIndex = 0;
        count--;
        if (itrs != null)
            itrs.elementDequeued();
        //唤醒入队锁
        notFull.signal();
        return x;
    }
    
  • peek()

    //只获取当前take位置的元素,不出队
    public E peek() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            return itemAt(takeIndex); // null when queue is empty
        } finally {
            lock.unlock();
        }
    }
    

LinkedBlockingDeque

  • 属性

    //头结点
    transient Node<E> first;
    //尾结点
    transient Node<E> last;
    //元素数量
    private transient int count;
    //队列容量
    private final int capacity;
    //重入锁
    final ReentrantLock lock = new ReentrantLock();
    //不为空条件
    private final Condition notEmpty = lock.newCondition();
    //不满条件
    private final Condition notFull = lock.newCondition();
    
    
  • offer()

    //头插法
    public boolean offerFirst(E e) {
        //e==null,则当前链表未初始化
        if (e == null) throw new NullPointerException();
        Node<E> node = new Node<E>(e);
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            //插入到链表头部
            return linkFirst(node);
        } finally {
            lock.unlock();
        }
    }
    
    //尾插法
    public boolean offerLast(E e) {
        if (e == null) throw new NullPointerException();
        Node<E> node = new Node<E>(e);
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            //插入链表尾部
            return linkLast(node);
        } finally {
            lock.unlock();
        }
    }
    
  • put

    public void putFirst(E e) throws InterruptedException {
        if (e == null) throw new NullPointerException();
        Node<E> node = new Node<E>(e);
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            while (!linkFirst(node))
                notFull.await();
        } finally {
            lock.unlock();
        }
    }
    
  • poll

    public E pollFirst() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            return unlinkFirst();
        } finally {
            lock.unlock();
        }
    }
    
  • take

    public E takeFirst() throws InterruptedException {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            E x;
            while ( (x = unlinkFirst()) == null)
                notEmpty.await();
            return x;
        } finally {
            lock.unlock();
        }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值