JUC--LinkedBlockingQueue源码分析 (基于JDK1.8) 未完待续

1 概述

LinkedBlockingQueue是一个有界性可选的阻塞队列,也就是再初始化的时候如果设置了大小,则大小就是固定的,否则队列的大小为Integer.MAX_VALUE。通常为了防止数据的膨胀,我们通常再初始化的时候给链表设置一个初始大小。

LinkedBlockingQueue是基于链表实现的一个FIFO队列,链表的首节点是添加时间最久的数据,而链表的尾节点是最早添加的数据,所以数据的添加是从尾节点开始。

2 使用示例

LinkedBlockingQueue比较适合于高并发的场景使用,具体原因我们会在文章后续内容中说明,接下来我们来看一下LinkedBlockingQueue的简用。

/**
 * 演示LinkedBlockingQueue的使用
 *
 * @author: LIUTAO
 * @Date: Created in 2018/9/28  11:15
 * @Modified By:
 */
public class LinkedBlockingQueueDemo {
    static  LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue(5);

    public static void main(String[] args) {

        //producer线程
        for (int i = 0; i < 10 ; i++){
            new Thread(() -> {
                try {
                    linkedBlockingQueue.put(Thread.currentThread().getName());
                    System.out.println(Thread.currentThread().getName() + "have put");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }

        //consumer线程
        for (int i = 0; i < 10 ; i++){
            new Thread(() -> {
                try {
                    System.out.println(linkedBlockingQueue.take() + "have got");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

可以看出LinkedBlockingQueue和ArrayBlockingQueue的使用方法基本相同,不同的是他们的使用场景。

3 类的继承关系

《JUC--LinkedBlockingQueue源码分析 (基于JDK1.8) 未完待续》

从上图我们可以看出LinkedBlockingQueue是一个阻塞队列,拥有阻塞队列的固有特性。

4 内部类


    static class Node<E> {
        E item;

        Node<E> next;

        Node(E x) { item = x; }
    }

链表节点类,用于表示链表的节点和存储节点数据。

5 属性

    //队列的容量
    private final int capacity;

    //队列中的当前元素数量
    private final AtomicInteger count = new AtomicInteger();

    //队列的头节点,头节点的数据为null
    transient Node<E> head;

    //队列的尾节点,尾节点的下一个节点为空
    private transient Node<E> last;

    //消费者锁
    private final ReentrantLock takeLock = new ReentrantLock();

    //消费者条件
    private final Condition notEmpty = takeLock.newCondition();

    //生产者锁
    private final ReentrantLock putLock = new ReentrantLock();

    //生产者条件
    private final Condition notFull = putLock.newCondition();

从上面的属性我们可以看出,与ArrayBlockingQueue相比,这里将生产者和消费者分开加锁,这样可以保证再高并发的情况下,生产者和消费者不相互影响,从而提高并发性。

6 构造函数

针对构造函数,我们还是来查看一下比较核心的函数。

public LinkedBlockingQueue(int capacity) {
        if (capacity <= 0) throw new IllegalArgumentException();
        this.capacity = capacity;
        last = head = new Node<E>(null);
    }

可以看出这个构造函数做的事比较简单,就初始化了队列大小,并设置了头尾节点,节点数据为null。

7 核心函数

针对LinkedBlockingQueue,我们依然来看一下它的put函数和take函数是怎么实现的。

7.1 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);
        final ReentrantLock putLock = this.putLock;
        final AtomicInteger count = this.count;
        putLock.lockInterruptibly();
        try {
            /*
             * Note that count is used in wait guard even though it is
             * not protected by lock. This works because count can
             * only decrease at this point (all other puts are shut
             * out by lock), and we (or some other waiting put) are
             * signalled if it ever changes from capacity. Similarly
             * for all other uses of count in other wait guards.
             */
            while (count.get() == capacity) {
                notFull.await();
            }
            enqueue(node);
            c = count.getAndIncrement();
            if (c + 1 < capacity)
                notFull.signal();
        } finally {
            putLock.unlock();
        }
        if (c == 0)
            signalNotEmpty();
    }

 

7.2 take函数

8 总结

 

 

    原文作者:JUC
    原文地址: https://blog.csdn.net/ONROAD0612/article/details/82872987
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞