In previous articles, we introduced the tools for concurrent programming in Java: The BlockingQueue interface, ArrayBlockingQueue, DelayQueue, LinkedBlockingQueue, PriorityBlockingQueue, and SynchronousQueue are the seventh articles in this series.

The BlockingDeque interface, like the BlockingQueue interface, is defined in java.util.Concurrent and represents a thread-safe “double-ended queue” to add or retrieve elements to the queue in a thread-safe manner. This article will take you through BlockingDeque.

Deque is short for “Double Ended Queue “. Therefore, “double-ended queue” means a queue in which elements can be inserted and removed from either end (the head or tail of the queue).

BlockingDeque can be used if a thread produces and consumes elements from the same queue. You can also use BlockingDeque only if the production thread needs to be inserted at both ends of the queue and the consuming thread needs to be removed from either end of the queue. Refer to the figure below to understand

A thread produces elements and inserts them to either end of the queue. If BlockingDeque is currently full, the insertion thread will block until the removal thread retrieves an element from the BlockingDeque. If BlockingDeque is currently empty, the removal thread will block until the inserter thread inserts an element into the BlockingDeque.

BlockingDeque method

BlockingDeque has four different sets of methods for inserting, deleting, and examining elements in the deque. Each set of methods also behaves differently in cases where the required action cannot be performed immediately. Refer to the table below

Team in the first operation An exception is thrown Return a specific value Block and wait Wait timeout after blocking
Insert the object addFirst(o) offerFirst(o) putFirst(o) offerFirst(o, timeout, timeunit)
Remove the object removeFirst(o) pollFirst() takeFirst() pollFirst(timeout, timeunit)
Check object Exists getFirst() peekFirst()
Of the operation An exception is thrown Return a specific value Block and wait Wait timeout after blocking
Insert the object addLast(o) offerLast(o) putLast(o) offerLast(o, timeout, timeunit)
Remove the object removeLast(o) pollLast() takeLast() pollLast(timeout, timeunit)
Check object Exists getLast() peekLast()

As you can see, these methods are similar to BlockingQueue except that xxxFirst and xxxLast are added to them

  1. Throw exception: Throw an exception if the result (empty or full queue) is not immediately responded to after the method is called.
  2. Return a specific value: If the method cannot respond immediately after being called (empty queue or full queue), a specific value (usually true/false) is returned, true indicating that the method succeeded, otherwise it failed.
  3. Block and wait: If a method is called and does not respond immediately (empty queue or full queue), the method will block and wait.
  4. Wait timeout after blocking: If a method is called and does not respond immediately to the result (empty queue or full queue), the method will be blocked and wait within a certain time range, that is, within the timeout range. When the timeout period is exceeded, the method thread will no longer block, but will return a specific value (usually true/ False), true indicating that the method executed successfully, otherwise indicating that the method failed.

BlockingDeque inheritance BlockingQueue

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

Below is a comparison of how BlockingQueue’s methods work in BlockingDeque implementation

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

BlockingDeque interface implementation class

BlockingDeque is an interface, so when we actually instantiate it, we need to implement the class using its interface. The LinkedBlockingDeque method in the java.util.Concurrent package implements the BlockingDeque interface.

It’s also used in much the same way as BlockingQueue, so I’ll cover it briefly here.

// Initializing a LinkedBlockingDeque BlockingDeque<String> deque = new LinkedBlockingDeque<String>(); deque.addFirst("1"); // Add element deque.addLast("2"); String two = deque.takelast (); String one = deque.takefirst (); // Get elements from the team leaderCopy the code

Welcome to my blog, where there are many fine collections

  • This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.

Feel helpful to you, help me like, share! Your support is my inexhaustible creative power! . In addition, the author recently a period of time output as follows boutique content, looking forward to your attention.

  • Spring Boot2.0 by Hand
  • Spring Security- JWT-OAUTH2
  • RBAC Authority Management System for Actual Combat Front-end and Back-end Separation
  • “Actual SpringCloud Micro-service from Bronze to King”
  • VUE Series