What is the ConcurrentLinkedDeque.offerLast() method in Java?

The ConcurrentLinkedDeque is thread-safe unbounded deque. The null value is not permitted as an element. We can use the ConcurrentLinkedDeque when multiple threads share a single deque.

The offerLast method can be used to add an element as the tail(end/last) element of the ConcurrentLinkedDeque object.

Syntax

public boolean offerLast(E e)

Parameters

The offerLast method takes the element to the deque as a parameter. This method throws NullPointerException if the argument is null.

Return value

The offerLast method returns true if the element is added to the deque.

Code

The code below demonstrates how to use the offerLast method:

import java.util.concurrent.ConcurrentLinkedDeque;
class OfferLast {
public static void main( String args[] ) {
ConcurrentLinkedDeque<String> deque = new ConcurrentLinkedDeque<>();
deque.offerLast("1");
System.out.println("The deque is " + deque);
deque.offerLast("2");
System.out.println("The deque is " + deque);
}
}

Explanation

In the above code:

  • In line 1, we import the ConcurrentLinkedDeque class.

  • In line 4, we create a ConcurrentLinkedDeque object with the name deque.

  • In line 5, we use the offerLast method of the deque object to add an element ("1") to the end of the deque. Then, we print it.

  • In line 8, we use the offerLast method to add an element ("2") to the end of the deque. Finally, we print it. Now the deque is [1,2].

Free Resources