[Concurrent container] 4 Introduction and selection of blocking/non-blocking queues

4 blocking/non-blocking queue

The blocking queue is a queue with blocking function, usually one end is put into producer data, and the other end is used for consumers to consume data.The blocking queue is thread-safe.

For blocking queues, we mainly focus on the blocking method (put/take), whether it is bounded, and the relationship with the thread pool.

Insert picture description here

Three sets of methods of BlockingQueue

  1. put, take: will block
  2. add, remove, element: will throw an exception
  3. offer, poll, peek: will return a boolean value, if the value is no value, it will return null

4.1 Common blocking queues

Insert picture description here

1 ArrayBlockingQueue bounded blocking queue

The characteristics of ArrayBlockingQueue are bounded, capacity can be specified, and fairness can be specified.

put method source code

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

2 LinkedBlockingQueue

Features are unbounded, default capacity is Integer.MAX_VALUE, internal structure (Node, there are two locks).

public void put(E e) throws InterruptedException {
    
    
    if (e == null) throw new NullPointerException();
    int c = -1;
    Node<E> node = new Node<E>(e);
    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();
}

3 PriorityBlockingQueue: Support priority, natural order, unbounded queue

4 SynchronousQueue

Features: The capacity is 0, the queue does not hold elements, and it is delivered directly, which is very efficient. It is a good concurrent data structure for direct delivery.

5 DelayQueue Delay queue, sorted according to delay time

4.2 Non-blocking queue

The only non-blocking queue in the concurrent package is ConcurrentLinkedQueue, which uses a linked list as a data structure and uses the CAS idea to achieve thread safety, which is suitable for concurrent scenarios with high performance requirements. Used less. The source code refers to the offer method.

4.3 Selection of blocking queue

Select the queue, you need to choose according to the actual scene.

  1. First consider the functional aspects, such as the need for prioritization, delayed execution, and so on.
  2. Then there is capacity, whether the capacity is fixed or unbounded, or capacity is not needed. ArrayBlockingQueue cannot be expanded.
  3. Memory structure: For example, the bottom layer of ArrayBlockingQueue is an array, and LinkedBlockingQueue is implemented internally with a linked list, which will generate many nodes.
  4. Throughput: Because LinkedBlockingQueue has two locks, its operation granularity is finer. When the degree of concurrency is high, the performance will be better than that of ArrayBlockingQueue with only one lock. SynchronousQueue is passed directly, and the performance is often better than other implementations.

Guess you like

Origin blog.csdn.net/LIZHONGPING00/article/details/114298441