Java Concurrent Programming Tool JUC Part Seven: BlockingDeque Double-ended Blocking Queue

In the previous article, I have introduced the tools of java concurrent programming: BlockingQueue interface, ArrayBlockingQueue, DelayQueue, LinkedBlockingQueue, PriorityBlockingQueue, SynchronousQueue. This article is the seventh article in the series.

BlockingDequeThe interface is defined in the BlockingQueuesame as the interface java.util.concurrent. It represents a thread-safe "double-ended queue", which adds elements to the queue or obtains elements in a thread-safe manner. This article will take everyone to learn more BlockingDeque.

dequeIt is an abbreviation of "Double Ended Queue". Therefore, the meaning of "double-ended queue" is a queue that can insert and remove elements from both ends (the head or the tail of the queue).

If a thread both produces and consumes elements of the same queue, then a deque can be used BlockingDeque. If the production thread needs to be inserted at both ends of the queue, and the consuming thread needs to be deleted from both ends of the queue, it is also possible to use only the BlockingDequedouble-ended queue. Refer to the figure below for understanding

A thread produces elements and inserts them into either end of the queue. If the BlockingDequecurrent is full, insert the thread will be blocked until the thread is removed from BlockingDequeremoving an element. If it is BlockingDequecurrently empty, the removing thread will be blocked until the inserting thread inserts an element into BlockingDequeit.

BlockingDeque method

BlockingDequeThere are 4 different methods for inserting, deleting and checking elements in the deque. Each group of methods behaves differently when the required operation cannot be executed immediately. Refer to the table below

Head of team operation Throw an exception Return a specific value Waiting after blocking Waiting timeout after blocking
Insert object addFirst(o) offerFirst(o) putFirst(o) offerFirst(o, timeout, timeunit)
Remove object removeFirst(o) pollFirst() takeFirst() pollFirst(timeout, timeunit)
Check object existence getFirst() peekFirst()
Tail operation Throw an exception Return a specific value Waiting after blocking Waiting timeout after blocking
Insert object addLast(o) offerLast (o) putLast (o) offerLast(o, timeout, timeunit)
Remove object removeLast(o) pollLast() takeLast () pollLast(timeout, timeunit)
Check object existence getLast() peekLast ()

As you can see, these methods BlockingQueueare similar to the methods, except that xxxFirst and xxxLast are added to the methods, so you can refer to my previous article for comparison). The meanings of the four behaviors of the above methods are

  1. Throw an exception : If the result cannot be responded to immediately after calling the method (empty queue or full queue), an exception will be thrown.
  2. Return a specific value : If the result cannot be responded to immediately after calling the method (empty queue or full queue), a specific value (usually true/false) is returned. True means the method is executed successfully, otherwise it means the method fails to execute.
  3. Waiting after blocking : If the method cannot be responded to the result immediately after calling the method (empty queue or full queue), the method will be blocked and kept in the waiting state.
  4. Waiting for timeout after blocking : If the method cannot be responded to the result immediately (empty queue or full queue) after calling the method, the method will be blocked waiting for a certain period of time, that is, blocked within the timeout period. When the timeout period expires, the method thread will no longer block, but return a specific value (usually true/false), true means that the method is executed successfully, otherwise it means that the method fails to execute.

BlockingDeque继承BlockingQueue

The BlockingDeque interface inherits the BlockingQueue interface. This means you can use BlockingDeque as a BlockingQueue. If you do this, the various insert methods will add the element to the end of the deque, and the remove method will remove the element from the head of the deque. The insert and delete methods of the BlockingQueue interface do just that.

The following is a comparison table of the role of the BlockingQueue method in the implementation of BlockingDeque

BlockingQueue BlockingDeque
add() addLast()
offer() offerLast ()
put() putLast ()
remove() removeFirst()
poll pollFirst()
take() takeFirst()
element() getFirst()
peek() peekFirst()

BlockingDeque interface implementation class

BlockingDequeIt is an interface, so when we actually instantiate it, we need to use its interface implementation class. java.util.concurrentThe LinkedBlockingDequemethods in the package implement the BlockingDequeinterface.

Its usage method is BlockingQueuesimilar to that of the same, so only a brief introduction is given here.

//初始化一个LinkedBlockingDeque
BlockingDeque<String> deque = new LinkedBlockingDeque<String>();

deque.addFirst("1");//向队首添加元素
deque.addLast("2"); //向队尾添加元素

String two = deque.takeLast(); //从队尾获取元素
String one = deque.takeFirst(); //从队首获取元素

Welcome to follow my blog, there are many boutique collections

  • This article is reproduced with an indication of the source (the connection must be attached, and the text cannot be reproduced only): Letter Brother Blog .

If you think it is helpful to you, please like and share it for me! Your support is my inexhaustible creative motivation! . In addition, the author has output the following high-quality content recently, and I look forward to your attention.

Guess you like

Origin blog.csdn.net/hanxiaotongtong/article/details/115291864