jdk源码解析三之LinkedBlockingQueue

LinkedBlockingQueue

一个基于链表的阻塞队列。此队列按 FIFO(先进先出)排序元素

   public LinkedBlockingQueue() {
        //默认最大容量
        this(Integer.MAX_VALUE);
    }

    public LinkedBlockingQueue(int capacity) {
        if (capacity <= 0) throw new IllegalArgumentException();
        this.capacity = capacity;
        //维护双端队列
        last = head = new Node<E>(null);
    }

put

//put将指定元素插入此队列尾部,将等待可用的空间
    public void put(E e) throws InterruptedException {
        if (e == null) throw new NullPointerException();
        // Note: convention in all put/take/etc is to preset local var
        // holding count negative to indicate failure unless set.
        int c = -1;
        //创建新节点
        Node<E> node = new Node<E>(e);
        //获取put锁
        final ReentrantLock putLock = this.putLock;
        final AtomicInteger count = this.count;
        //如果当前线程未被中断,则获取锁。
        putLock.lockInterruptibly();
        try {
            //达到上限容量,则一直等待
            while (count.get() == capacity) {
                notFull.await();
            }
            //设置值
            enqueue(node);
            c = count.getAndIncrement();
            if (c + 1 < capacity)
                //列是否有可用空间,如果有则唤醒一个等待线程
                notFull.signal();
        } finally {
            //释放锁
            putLock.unlock();
        }
        // 如果队列中有一条数据,唤醒消费线程进行消费
        if (c == 0)
            signalNotEmpty();
    }

offer

    public boolean offer(E e) {
        if (e == null) throw new NullPointerException();
        //等于最大容量,则返回,而不阻塞
        final AtomicInteger count = this.count;
        if (count.get() == capacity)
            return false;

        int c = -1;
        Node<E> node = new Node<E>(e);
        final ReentrantLock putLock = this.putLock;
        //因为不阻塞,所以直接获取锁
        putLock.lock();
        try {
            //再次检查容量大小,然后直接添加,随后唤醒一个等待线程
            if (count.get() < capacity) {
                enqueue(node);
                c = count.getAndIncrement();
                if (c + 1 < capacity)
                    notFull.signal();
            }
        } finally {
            putLock.unlock();
        }
        // 如果队列中有一条数据,唤醒消费线程进行消费
        if (c == 0)
            signalNotEmpty();
        return c >= 0;
    }

阻塞时间的offer

 public boolean offer(E e, long timeout, TimeUnit unit)
        throws InterruptedException {

        if (e == null) throw new NullPointerException();
        long nanos = unit.toNanos(timeout);
        int c = -1;
        final ReentrantLock putLock = this.putLock;
        final AtomicInteger count = this.count;
        //获取中断锁
        putLock.lockInterruptibly();
        try {
            //等于最大容量,则一直循环
            while (count.get() == capacity) {
                //超过超时时间则返回
                if (nanos <= 0)
                    return false;
                //当前线程在接到信号、被中断或到达指定等待时间之前一直处于等待状态。
                nanos = notFull.awaitNanos(nanos);
            }
            enqueue(new Node<E>(e));
            c = count.getAndIncrement();
            //通知信号
            if (c + 1 < capacity)
                notFull.signal();
        } finally {
            putLock.unlock();
        }
        // 如果队列中有一条数据,唤醒消费线程进行消费
        if (c == 0)
            signalNotEmpty();
        return true;
    }

take

//获取并移除此队列的头部,在元素变得可用之前一直等待
 public E take() throws InterruptedException {
        E x;
        int c = -1;
        final AtomicInteger count = this.count;
        final ReentrantLock takeLock = this.takeLock;
        //中断点
        takeLock.lockInterruptibly();
        try {
            //队列为空,阻塞等待
            while (count.get() == 0) {
                notEmpty.await();
            }
            //获取值
            x = dequeue();
            c = count.getAndDecrement();
            // 队列中还有元素,唤醒下一个消费线程进行消费
            if (c > 1)
                notEmpty.signal();
        } finally {
            takeLock.unlock();
        }
        // 之前队列是满的,则唤醒生产线程进行添加元素
        if (c == capacity)
            signalNotFull();
        return x;
    }

 private E dequeue() {
        // assert takeLock.isHeldByCurrentThread();
        // assert head.item == null;
         //head默认root的value是null
        Node<E> h = head;
        Node<E> first = h.next;
        // head节点原来指向的节点的next指向自己,等待下次gc回收
        h.next = h; // help GC
        // head节点指向下一个节点
        head = first;
        //获取新的head的value
        E x = first.item;
        //新的head设置null
        first.item = null;
        return x;
    }

poll

    public E poll() {
        final AtomicInteger count = this.count;
        //容量为0,直接返回
        if (count.get() == 0)
            return null;
        E x = null;
        int c = -1;
        final ReentrantLock takeLock = this.takeLock;
        takeLock.lock();
        try {
            if (count.get() > 0) {
                x = dequeue();
                c = count.getAndDecrement();
                if (c > 1)
                    notEmpty.signal();
            }
        } finally {
            takeLock.unlock();
        }
        if (c == capacity)
            signalNotFull();
        return x;
    }

peek

    public E peek() {
        if (count.get() == 0)
            return null;
        final ReentrantLock takeLock = this.takeLock;
        takeLock.lock();
        try {
            Node<E> first = head.next;
            if (first == null)
                return null;
            else
                return first.item;
        } finally {
            takeLock.unlock();
        }
    }

remove

    public boolean remove(Object o) {
        //为null,直接返回
        if (o == null) return false;
        //put和take锁,就暂时不能新增或修改
        fullyLock();
        try {
            for (Node<E> trail = head, p = trail.next;
                 p != null;
                 trail = p, p = p.next) {
                //匹配到值,则删除
                if (o.equals(p.item)) {
                    unlink(p, trail);
                    return true;
                }
            }
            return false;
        } finally {
            //释放2个锁
            fullyUnlock();
        }
    }

    void unlink(Node<E> p, Node<E> trail) {
        // assert isFullyLocked();
        // p.next is not changed, to allow iterators that are
        // traversing p to maintain their weak-consistency guarantee.
        p.item = null;
          //在迭代的时候,如果p.next为null,则会造成异常.所以这里没设置null
        trail.next = p.next;
        if (last == p)
            last = trail;
        // 如果删除之前元素是满的,删除之后就有空间了,唤醒生产线程放入元素
        if (count.getAndDecrement() == capacity)
            notFull.signal();
    }

迭代器

当执行迭代器的nextNode的时候,如果同时发现有执行take操作,因为当前head.next指向了自己,
在这里插入图片描述

        private Node<E> nextNode(Node<E> p) {
            for (;;) {
                Node<E> s = p.next;
                //take时,head.next=head,则直接返回当前head的下一个节点
                if (s == p)
                    return head.next;
                if (s == null || s.item != null)
                    return s;
                p = s;
            }
        }

总结

底层阻塞队列FIFO.内部由两个ReentrantLock来实现出入队列的线程安全,由各自的Condition对象的await和signal来实现等待和唤醒功能。
默认容量无界,且底层链表,所以执行插入和删除效率比较高.且2把锁维护新增删除,所以并发有所提高.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值