Blocking queue BlockingQueue actual combat and its principle analysis

1. Introduction to BlockingQueue

1. Queue (queue) top-level interface

add(E) adds an element, returns true if successful, otherwise returns an exception

offer(E) Add elements, return true if successful, otherwise return false

remove() returns and removes the head element of the queue, and returns an exception if the queue is empty

poll() returns and removes the first element of the queue, and returns null if the queue is empty

peek() Get the first element of the queue, return null if the queue is empty

BlockingQueue inherits from Queue and provides blocking features. There are common methods for entering and exiting the queue

Enqueue: put(E) offer(E) offer(E,long,TimeUnit)

出队:poll(),poll(long,TimeUnit),take()

1.1 Common methods of BlockingQueue in practice

There are four commonly used methods in the demonstration: add remove offer poll is used, which conforms to the first-in-first-out characteristics of the queue.

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class BlockingQueueTest {

    public static void main(String[] args) {

        //测试add remove offer poll
        Queue addblockingQueue = new ArrayBlockingQueue(2);
        addblockingQueue.add(1);
        addblockingQueue.add(2);
//        addblockingQueue.add(3);//放入第三个Exception in thread "main" java.lang.IllegalStateException: Queue full

//        addblockingQueue.remove();
//        addblockingQueue.remove();
//        addblockingQueue.remove();//移出第三个Exception in thread "main" java.util.NoSuchElementException

        Queue offerblockingQueue = new ArrayBlockingQueue(2);
        System.out.println(offerblockingQueue.offer(1));//返回true
        System.out.println(offerblockingQueue.offer(2));//返回true
        System.out.println(offerblockingQueue.offer(3));//返回false

        System.out.println(offerblockingQueue.poll());//返回1
        System.out.println(offerblockingQueue.poll());//返回1 符合队列特性先进先出
        System.out.println(offerblockingQueue.poll());//返回null
    }
}

1.2 Commonly used queues

Queue application scenarios: Queues are thread-safe, and we often use them in thread pools, middleware such as producer-consumer mode, and nacos registry. 

1.2.1  Introduction to ArrayBlockingQueue (TODO)

 ArrayBlockingQueue is a bounded blocking queue. The bottom layer is based on an array implementation. It uses reenTrantLock to ensure thread safety. It is a good choice to use it in general. However, in high-concurrency scenarios, because reenTrantLock is used to lock and unlock, the speed of insertion and extraction It will be called a performance bottleneck. LinkedBlockingQueue is generally used in high-concurrency scenarios. See 1.1 for the actual combat code

Source code analysis focus:

1. Construction method

2. Elements used: array, reenTrantLock, two conditional queues (not full and empty)

3. put and take methods

 1.2.2 Introduction to linkedBlockingQueue (TODO)

linkedBlockingQueue is a bounded blocking queue implemented by a linked list, but its default maximum value is Integer.MAX_VALUE, which is a very large number, so we often say it is an unbounded blocking queue. When the memory is insufficient, an OOM problem will occur, so in order to solve this problem, the size of the queue is generally specified during initialization. linkedBlockingQueue also uses ReentrantLock to ensure thread safety. The difference from arrayBlockingQueue is that it has two locks, one for entering the queue and one for exiting the queue. There are also two conditional queues to ensure the wake-up of entering and exiting the queue .

Source code analysis focus:

1. Construction method

2. Elements used

3. put,take,remove method

1.2.3 Introduction to LinkedBlockingDeque

Deque is a blocking double-ended queue with similar characteristics to linkedBolckingQueue. The difference with linkedBolckingQueue is that it only has a reentrantLock lock and has several more APIs that can operate the elements at the head and tail of the queue.

addFirst(E e) 
addLast(E e) 
offerFirst(E e) 
offerLast(E e)

 

 

Guess you like

Origin blog.csdn.net/qq_21575929/article/details/124774267