What is a blocking queue

When the queue is empty, the thread that fetched the element waits for the queue to become non-empty. When the queue is full, the thread that stores the element waits for the queue to become available. Blocking queues are often used in producer and consumer scenarios, where the producer is the thread that adds elements to the queue and the consumer is the thread that takes elements from the queue. In Java, BlockingQueue is an interface, Its implementation classes include ArrayBlockingQueue, DelayQueue, LinkedBlockingDeque, LinkedBlockingQueue, PriorityBlockingQueue, SynchronousQueue, etc. They differ mainly in storage structures or operations on elements

Commonly used blocking queues

The queue boundedness Whether the lock The data structure
ArrayBlockingQueue Bounded bounded lock ArrayList
LinkedBlockingQueue obtionally-bounded lock LinkedList
SynchronousQueue Bounded bounded lock
LinkedTransferQueue Unbounded unbounded lock Heap
PriorityBlockingQueue Unbounded unbounded lock Heap
DelayQueue Unbounded unbounded lock Heap
LinkedBlockingDeque Unbounded unbounded unlocked Heap

ArrayBlockingQueue

A bounded blocking queue implemented in an array that sorts elements on a first-in, first-out (FIFO) basis. Fair and unfair locks are supported.

LinkedBlockingQueue

A bounded queue consisting of a linked list structure with a length of integer.max_value. This queue is sorted in first-in, first-out order.

SynchronousQueue

Is a blocking queue that does not store elements, and every PUT operation must wait for a take operation, otherwise elements cannot be added. Fair and unfair locks are supported. One use case for SynchronousQueue is in thread pools. Executors. NewCachedThreadPool used SynchronousQueue will (), the thread pool (a new task comes) according to need to create a new thread, if there is a idle thread will be repeated use, thread idle is recycled after 60 seconds.

LinkedTransferQueue

Is an unbounded blocking queue composed of a linked list structure, equivalent to other queues, LinkedTransferQueue queue with transfer and tryTransfer methods.

PriorityBlockingQueue

CompareTo () is an unbounded queue that supports thread priority sorting. The default order is natural. You can also customize the compareTo() method to specify the order of elements.

DelayQueue

An unbounded queue that implements PriorityBlockingQueue to implement delayed fetching. When creating an element, you can specify how long it takes to fetch the current element from the queue. Elements cannot be fetched from the queue until the delay expires

Usage Scenarios:

  1. Scheduled task scheduling. DelayQueue is used to store the tasks that will be executed on that day and the execution time. Once a task is obtained from DelayQueue, it will be executed. For example, TimerQueue is implemented using DelayQueue.
  2. The design of the cache system: we can use the DelayQueue to store the expiration date of cached elements, and use a thread to query the DelayQueue. Once the element can be retrieved from the DelayQueue, the expiration date of the cache is up.

LinkedBlockingDeque

A two-way blocking queue consisting of a linked list structure. Elements can be added and removed from both the header and tail of the queue, reducing lock contention to at most half when multi-threaded

Commonly used method

methods An exception is thrown Return special value Has been blocked Timeout exit
insert add(e) offer(e) put(e) offer(e, tiime, unit)
remove remove() poll() take() poll(tiime, unit)
To view element() peek()

An exception is thrown

An IllegalStateException(” Queue Full “) is thrown when an element is inserted into the blocking Queue when the Queue is full. When the queue is empty, NoSuchElementException is thrown when an element is fetched from the queue.

Return special value

The insert method returns whether it succeeded, and true on success. Remove method, which removes an element from the queue or returns NULL if none is present.

Has been blocked

When the blocking queue is full, if the producer thread puts elements into the queue, the queue will block the producer thread until it gets data or exits in response to an interrupt. When the queue is empty, the consumer thread tries to take elements from the queue, and the queue blocks the consumer thread until the queue is available.

Timeout exit

When the blocking queue is full, the queue blocks the producer thread for a certain amount of time, and if it is longer than a certain amount of time, the producer thread exits.

reference

Blog.csdn.net/KevinChen20… www.jianshu.com/p/7b2f1fa61…