What is the ConcurrentLinkedDeque.add() method in Java?
The
ConcurrentLinkedDequeis a thread-safe, unbounded deque. Thenullvalue is not permitted as an element. We can use theConcurrentLinkedDequewhen multiple threads share a singledeque.
The add method is used to add an element at the tail (end) of the ConcurrentLinkedDeque object.
Syntax
public boolean add(E e)
Parameters
The add method takes the element to be added to the deque as a parameter. This method throws a NullPointerException error if the argument is null.
Return value
The add method returns True if the element is successfully added to the deque.
Code
The code below demonstrates how to use the add method.
import java.util.concurrent.ConcurrentLinkedDeque;class Add {public static void main( String args[] ) {ConcurrentLinkedDeque<String> deque = new ConcurrentLinkedDeque<>();deque.add("1");System.out.println("The deque is " + deque);deque.add("2");System.out.println("The deque is " + deque);}}
Code explanation
In the code above:
-
In line 1, we import the
ConcurrentLinkedDequeclass. -
In line 4, we create a
ConcurrentLinkedDequeobject with the namedeque. -
In line 5, we use the
addmethod of thedequeobject to add an element ("1") to the end of thedeque. Then, we print thedeque. -
In line 8, we use the
addmethod to add an element ("2") to the end of thedequeand print it. Now, the deque is[1,2].