List of high-quality authors
J**A Concurrency Basics: LinkedBlockingDeque Full Analysis! - programmer Goodelinkedblockingdeque
It provides a thread-safe implementation of double-ended queues, which supports efficient insertion and removal operations at both ends of the queue, and has a blocking function that can well coordinate the speed difference between producers and consumers, and its internal linked list structure makes excellent concurrency performance, which is ideal for handling data transfer between multiple threads.
linkedblockingdeque
Implement a thread-safe double-ended queue (deque), which can add and remove elements at both ends, and it is blocked, which means that when the queue is empty, if the thread tries to take an element from the queue, the thread will be blocked until there is an element in the queue to be removed; Similarly, if the queue is full, threads trying to add elements will be blocked until there is space in the queue for adding new elements.
Take a practical case in life, for example, in a bakery, the baker is responsible for producing bread (producer), and the customer comes to the store to buy bread (consumer), and after the baker makes the bread, he will put it on a display shelf for the customer to choose; Customers take the bread they want from this display rack and use it herelinkedblockingdeque
to simulate this scenario. The baker (producer thread) puts the freshly made bread at one end of the queue (adding elements to the queue), while the customer (the consumer thread) takes the bread from the other end of the queue (removing elements from the queue):
Blocking characteristics: If there is no bread on the display shelf (the queue is empty), the customer will be blocked until the baker makes new bread and put it on the display shelf, similarly, if the display shelf is full (the queue is full), the baker will be blocked until a customer has picked up some bread and made room for new bread. Double-ended operationIn this scenario, while usually the baker only places bread at one end and the customer picks up the bread at the other end, the flexibility of the double-end queue means that this behavior can be easily changed, for example, if there are special circumstances, the baker can retrieve some bread from the display rack (remove elements from the other end of the queue), or customers can pre-place their bread orders on the display rack (add elements at the other end of the queue). linkedblockingdeque
is a thread-safe double-ended queue that allows elements to be added and removed from both ends of the queue, and it is blocking, which is commonly used to solve the following problems:
Thread-safe: In a multithreaded environment, when multiple threads need to access and modify shared datalinkedblockingdeque
It provides a thread-safe way to store and retrieve this data, and its internal synchronization mechanism ensures the consistency and integrity of the data. Blocking operation: When the queue is empty, the consumer thread invokestake()
Methods are blocked until the producer thread adds elements to the queue, and again, when the queue is full, the producer thread callsput()
Methods are also blocked until the consumer thread removes the element from the queue, which helps prevent the thread from idling or wasting CPU resources unnecessarily. Capacity limitslinkedblockingdeque
You can specify a maximum capacity at creation time, which limits the number of elements that can be stored in the queue, helps prevent memory overflows, and when the queue reaches its maximum capacity, the producer thread is blocked until space is available in the queue. Double-ended operation: with the ordinaryblockingqueue
interface implementation,linkedblockingdeque
The ability to double-ended queues is provided, allowing elements to be added and removed from both ends of the queue, which provides greater flexibility for some specific use cases. Efficient concurrency performance: Due to its internal use of a linked list data structurelinkedblockingdeque
It generally has good performance when handling a large number of concurrent operations, and it is suitable for producer-consumer scenarios that require high throughput and low latency. Here's a simple example that demonstrates how to use itlinkedblockingdeque
class, as follows**:
import j**a.util.concurrent.blockingqueue;
import j**a.util.concurrent.linkedblockingdeque;
public class linkedblockingdequeexample
catch (interruptedexception e)
Start a consumer thread to remove elements from the queue
thread consumer = new thread(()
catch (interruptedexception e)
Start the producer and consumer threads
producer.start();
consumer.start();
Wait for the execution of the two threads to complete
producer.join();
consumer.join();
system.out.println("producer and consumer threads h**e finished.");
In the above, one was createdlinkedblockingdeque
instance, and specified its maximum capacity of 5, and then creates a producer thread and a consumer thread, and the producer thread will loop 10 times, producing a string each time and putting it in the queue if the queue is fullput
The method will block until space is available in the queue, and the consumer thread will loop 10 times each time, taking an element from the queue and printing it, if the queue is emptytake
The method will block until there is an element in the queue that is desirable.
Since the speed of the producer and consumer threads may be different,linkedblockingdeque
Being a blocking queue is able to reconcile the speed difference between these two threads, ensuring that they can work together without causing either side to stall because the queue is empty or full.
linkedblockingdeque
Realizedblockingdeque
interface, which is a thread-safe double-ended queue, is as follows:linkedblockingdeque
Meaning of some of the main methods in the class:
add(e e)Inserts the specified element into the queue represented by this double-ended queue (i.e., at the tail of this double-ended queue) and returns on success if it is immediately feasible and does not violate the capacity limittrue
, if there is currently no space available, it is thrownillegalstateexception
This isqueue
interface. offer(e e)Inserts the specified element into the queue represented by this double-ended queue (i.e., at the tail of this double-ended queue) and returns on success if it is immediately feasible and does not violate the capacity limittrue
and return if there is currently no space availablefalse
This isqueue
interface. put(e e)throws interruptedException inserts the specified element into the queue represented by this double-ended queue (i.e. at the tail of this double-ended queue), and if necessary, waits for space to become available, which isblockingqueue
interface. offer(e e, long timeout, timeunit unit)The specified element is inserted into the queue represented by this double-ended queue, and if necessary, the specified amount of time will be waited for for the space to become available, which isblockingqueue
interface. remove()Gets and removes the head of the queue represented by this double-ended queue, and throws it if this double-ended queue is emptynosuchelementexception
This isqueue
interface. poll()Gets and removes the head of the queue represented by this double-ended queue, and returns if this double-ended queue is emptynull
This isqueue
interface. take()throws interruptedException gets and removes the head of the queue represented by this double-ended queue, waiting until the element becomes available, which isblockingqueue
interface. poll(long timeout, timeunit unit)Gets and removes the head of the queue represented by this double-ended queue and waits for the element to become available for a specified amount of time, which isblockingqueue
interface. peek()Gets, but does not remove, the head of the queue represented by this double-ended queue, and returns if this double-ended queue is emptynull
This isqueue
interface. element()Gets, but does not remove, the head of the queue represented by this double-ended queue, which isqueue
interface. push(e e)Push the element into the stack represented by this double-ended queue (i.e., the head of this double-ended queue) and return on success if it is immediately feasible and does not violate the capacity limittrue
, if there is currently no space available, it is thrownillegalstateexception
pop()An element is ejected from the stack represented by this double-ended queue and thrown if this double-ended queue is emptynosuchelementexception
addfirst(e e)addlast(e e)Inserts the specified element at the beginning or end of this double-ended queue. offerfirst(e e)offerlast(e e)Inserts the specified element at the beginning or end of this double-ended queue and returns on success if it is immediately feasible and does not violate the capacity limittrue
and return if there is currently no space availablefalse
removefirst()removelast()Gets and removes the first or last element of this double-ended queue. pollfirst()polllast()Gets and removes the first or last element of this double-ended queue, and returns if the double-ended queue is emptynull
getfirst()getlast()Gets, but does not remove, the first or last element of this double-ended queue. peekfirst()peeklast()Gets but does not remove the first or last element of this double-ended queue, and returns if this double-ended queue is emptynull
J**A Concurrency Basics: LinkedBlockingDeque Full Analysis! - programmer Goodelinkedblockingdeque
It combines the characteristics of blocking queues and double-ended queues, which has the advantage of efficient concurrency performance and flexible two-end operations, which is suitable for use in producer-consumer scenarios, and can well handle data sharing and transmission between multiple threads, and the disadvantages are that if the queue size is not set properly under high concurrency, it may lead to too many thread blocking, affecting the overall performance of the system, in addition, because it is based on the implementation of linked lists, its memory footprint may be relatively high.
Follow me and learn Internet programming techniques every day - programmer Goode end!
J**A Concurrency Basics: LinkedBlockingDeque Full Analysis!
J**A Concurrency Basics: LinkedTransferQueue Comprehensive Analysis!
J**A Concurrency Basics: LinkedBlockingQueue Full Analysis!
J**a Concurrency Basics: What Are the Differences Between Deque and Queue Interfaces?
Spring Core Basics: A comprehensive summary of the basic tool classes provided in Spring!