Concurrent Linked Deque in Java with Examples
Introduction
Java's concurrent-linked deque, which holds its items as linked nodes, is unconstrained and thread-safe. Concurrent Linked Deque allows for element removal and addition on both sides because it implements the deque interfaces.
The Abstract Collection class and the Collection interface are implemented by the Java Concurrent Linked Deque class, which is a component of the Java Collection Framework. The java. util. concurrent package contains it. Concurrently using LinkedList, it is utilized to implement Deque.
When numerous threads need to access the same collection, a concurrent linked deque in Java is a good option since concurrent inserting, deletion, and reading operations run securely throughout many threads. Keep in mind that it doesn't block functions as the Blocking Dequeue interface does.
A component of the Java Collections Framework is this concurrent LinkedList de queue class. effects of memory accuracy. Like other concurrent collections, operations in one thread that precede adding an item to a Concurrent Linked Deque occur before actions in another thread that access or remove that element from the Concurrent Linked Dequeue.
Declaration of Concurrent Linked Dequeue
It complies with the interfaces for Serializable, Iterated, Collection, Deque, and Queue. the declaration syntax for concurrent linked dequeue is as follows
ConcurrentLinkedDequeue < X > cd = new ConcurrentLinkedDequeue < X > ();
Where here, X is the maintained elements
Concurrent Linked Dequeue Constructors
Concurrent linked dequeue implements two distinct kinds of constructors. While some of the methods accept parameters in the form of collections, others only accept an empty dequeue. The concurrent linked de queue is built using the methods listed above.
The constructors implemented by concurrent linked dequeue are
- ConcurrentLinkedDequeue ()
- ConcurrentLinkedDequeue (collection a)
Example program for concurrent linked dequeue constructor is as follows
Code
// Example Java Program to implement basic Concurrent Linked Deque
import java. io. *;
import java. util. *;
import java. util. concurrent. *;
class Demo
{
public static void main (String [] s)
{
// Declare the Concurrent Linked Deque using empty ConcurrentLinkedDequeue () method
ConcurrentLinkedDeque<String> c = new ConcurrentLinkedDeque<String>();
// insert items on the front by using add the first method
c. add First("A");
c. add First("B");
c. add First("C");
c. add First("D");
c. add First("E");
// Concurrent Linked Deque present items are printed
System. out. print (" Concurrent Linked Deque elements == " + c);
// Now the other concurrent linked dequeue is declared using the collection constructor
ConcurrentLinkedDeque<String> c1 = new ConcurrentLinkedDeque<String>(c);
// Concurrent Linked Deque present items are printed
System. out. print (" new Concurrent Linked Deque elements are: " + c1);
// now all the elements are printed once again of the existing concurrent linked dequeue
System. out. print (" The elements of Concurrent Linked Deque are == " + c);
// printing the first element using the peek First () technique
System. out. print ("The First item in the queue is == " + c. peek First ());
// get Last () method to print the last element
System. out. print ("The Last item in the queue is == " + c. get Last ());
// remove Last () method to remove the last element
c. remove Last ();
System. out. print (“The Last Element removed from the queue”);
// printing the updated Concurrent Linked Deque
System. out. print (" The updated elements of the queue are == " + c);
}
}
Output
C:\java>javac Demo.java
C:\java>java Demo
Concurrent Linked Deque elements == [E, D, C, B, A]
new Concurrent Linked Deque elements are: [E, D, C, B, A]
The elements of Concurrent Linked Deque are == [E, D, C, B, A]
The first item in the queue is == E
The last item in the queue is == A
The last Element removed from the queue
The updated elements of the queue are == [E, D, C, B]
Operations of Concurrent Linked Deque
Just like other queues the basic functions that are performed on the concurrent linked de queue are inserting the elements, removing the elements, and also access and retrieval of items respectively.
The operations are
- Adding
- Removing
- Accessing
- Iterating
Concurrent Linked Deque can be iterated using the descending Iterator () or iterator () methods.
Let’s see an example program for implementing iteration of concurrent linked dequeue
Code
// code for implementing iteration items of Concurrent Linked Deque
import java. io. *;
import java. util. *;
import java. util. concurrent. *;
public class Demo
{
public static void main (String s [])
{
ConcurrentLinkedDeque<Integer> c = new ConcurrentLinkedDeque<Integer>();
c. add (6);
c. add (43);
c. add (11);
c. add (90);
c. add (65);
// prints the concurrent linked dequeue
System. out. println (" The elements present in concurrent linked dequeue are “+ c);
// iterator 1 is created
Iterator it = c. iterator ();
System. out. println ("The iterated items are ");
while (it. has Next ())
{
System. out. println (it. next ());
}
// iterator 2 is created to read and print elements in reversing the order
Iterator it2 = c. descending Iterator ();
System. out. println (" The iterated items in reverse order are ");
while (it2. has Next ())
{
System. out. println (it2. next());
}
}
}
Output
C:\java>javac Demo.java
C:\java>java Demo
The elements present in concurrent linked dequeue are [6, 43, 11, 90, 65]
The iterated items are
6
43
11
90
65
The iterated items in reverse order are
65
90
11
43
6