The
ConcurrentLinkedDeque
is thread-safe unbounded deque. Thenull
value is not permitted as an element. We can use theConcurrentLinkedDeque
when multiple threads share a singledeque
.
The offerLast
method can be used to add an element as the tail(end/last) element of the ConcurrentLinkedDeque
object.
public boolean offerLast(E e)
The offerLast
method takes the element to the deque
as a parameter. This method throws NullPointerException
if the argument is null
.
The offerLast
method returns true
if the element is added to the deque
.
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);}}
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]
.