Java Program to Implement ConcurrentLinkedQueue API Last Updated : 24 Jun, 2021 Comments Improve Suggest changes Like Article Like Report The ConcurrentLinkedQueue class in Java is a part of the Java Collection Framework. It belongs to java.util.concurrent package. It was introduced in JDK 1.5. It is used to implement Queue with the help of LinkedList concurrently. It is an unbounded thread-safe implementation of Queue which inserts elements at the tail of the Queue in a FIFO(first-in-first-out) fashion. It can be used when an unbounded Queue is shared among many threads. To implement ConcurrentLinkedQueue API first we create a class "ConcurrentLinkedQueueImplmentation" and create all the methods of the queue in this class. Implementation of the ConcurrentLinkedQueue API: Java // Java program to demonstrate the // implementation of ConcurrentLinkedQueue API import java.util.Collection; import java.util.Iterator; import java.util.concurrent.ConcurrentLinkedQueue; class ConcurrentLinkedQueueImplmentation<E> { private ConcurrentLinkedQueue<E> concurrentLinkedQueue; // Constructor creates a new empty ConcurrentLinkedQueue public ConcurrentLinkedQueueImplmentation() { // New empty concurrentLinkedQueue concurrentLinkedQueue = new ConcurrentLinkedQueue<>(); } // Constructor creates a new ConcurrentLinkedQueue of // type E public ConcurrentLinkedQueueImplmentation(Collection<? extends E> c) { // New empty concurrentLinkedQueue concurrentLinkedQueue = new ConcurrentLinkedQueue<>(c); } // Add specified element to the tail of the queue public boolean add(E e) { // Add element return concurrentLinkedQueue.add(e); } // Returns true if the queue contains the specified // element public boolean contains(Object o) { return concurrentLinkedQueue.contains(o); } // Returns an iterator over the elements of the queue public Iterator<E> iterator() { return concurrentLinkedQueue.iterator(); } // Add the specified element at the tail of the queue public boolean offer(E e) { return concurrentLinkedQueue.offer(e); } // Returns the peek of the queue or returns null if this // queue is empty. public E peek() { return concurrentLinkedQueue.peek(); } // Retrieves and removes the head of this queue, or // returns null if this queue is empty public E poll() { return concurrentLinkedQueue.poll(); } // Removes a single instance of the specified element // from this queue, if it is present. public boolean remove(Object o) { return concurrentLinkedQueue.remove(o); } // Returns the size of the queue public int size() { return concurrentLinkedQueue.size(); } // Returns an array containing all of the elements in // this queue public Object[] toArray() { return concurrentLinkedQueue.toArray(); } // Returns an array containing all of the elements in // this queue, in proper sequence; the return type of // the array is that of the specified // array. public <T> T[] toArray(T[] a) { return concurrentLinkedQueue.toArray(a); } } public class GFG { public static void main(String[] arg) { // New ConcurrentLinkedQueue ConcurrentLinkedQueueImplmentation<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueueImplmentation< Integer>(); // Adding elements to the ConcurrentLinkedQueue concurrentLinkedQueue.add(10); concurrentLinkedQueue.add(20); concurrentLinkedQueue.add(30); concurrentLinkedQueue.add(40); concurrentLinkedQueue.add(50); System.out.println( "The elements of the ConcurrentLinkedQueue is:"); // Iterator to iterate over ConcurrentLinkedQueue Iterator<Integer> it = concurrentLinkedQueue.iterator(); // Iterate over ConcurrentLinkedQueue while (it.hasNext()) { System.out.print(it.next() + " "); } System.out.println(); // Print the size of the queue System.out.println("Size of the concurrentLinkedQueue is: " + concurrentLinkedQueue.size()); System.out.println(); // Print Peek element of the queue System.out.println("The peek element of the concurrentLinkedQueue is: " + concurrentLinkedQueue.peek()); System.out.println(); // Print the polled element of the queue System.out.println("The polled element of the concurrentLinkedQueue is: " + concurrentLinkedQueue.poll()); System.out.println(); // Remove specified element to the queue, returns // true if removed successfully or returns null if // element not present in the queue System.out.println("Remove 30: " + concurrentLinkedQueue.remove(30)); System.out.println(); // Check whether the spaecified element is present // or not in the queue System.out.println("The concurrentLinkedQueue contains 40:" + concurrentLinkedQueue.contains(40)); System.out.println(); // Print the size of the queue System.out.println("Size of the concurrentLinkedQueue is: " + concurrentLinkedQueue.size()); } } OutputThe elements of the ConcurrentLinkedQueue is: 10 20 30 40 50 Size of the concurrentLinkedQueue is: 5 The peek element of the concurrentLinkedQueue is: 10 The polled element of the concurrentLinkedQueue is: 10 Remove 30: true The concurrentLinkedQueue contains 40:true Size of the concurrentLinkedQueue is: 3 Comment More infoAdvertise with us Next Article Java Program to Implement ConcurrentLinkedQueue API K KapilChhipa Follow Improve Article Tags : Java Java Programs Java-Collections Java-ConcurrentLinkedQueue Java-concurrent-package +1 More Practice Tags : JavaJava-Collections Similar Reads Java Program to Implement ConcurrentLinkedDeque API ConcurrentLinkedDeque class in Java is an unbounded concurrent deque that stores its elements as linked nodes where each node contains the address of the previous as well as next nodes. It belongs to java.util.concurrent package. This class is a member of the Java Collections Framework. It also exte 3 min read Java Program to Implement ConcurrentSkipListMap API ConcurrentSkipListMap API is based on the implementation of the ConcurrentNavigableMap. This map is sorted according to the natural ordering of its keys or by a Comparator provided on the map during creation time. This class implements a concurrent variant of SkipLists which makes its expected avera 10 min read Java Program to Implement ConcurrentHashMap API ConcurrentHashMap class obeys the same functional specification as HashTable and includes all the versions of methods corresponding to each method of a HashTable. A HashTable supports the full concurrency of retrievals and adjustable concurrency for updates. All the operations of ConcurrentHashMap a 6 min read Java Program to Implement DelayQueue API The DelayQueue class belongs to java.util.concurrent package. DelayQueue implements the BlockingQueue interface. DelayQueue is a specialized Priority Queue that orders elements supported their delay time. It means that only those elements can be taken from the queue whose time has expired. DelayQueu 7 min read Java Program to Implement ArrayBlockingQueue API ArrayBlockingQueue class is a member of the Java Collection framework. ArrayBlockingQueue is a bounded blocking queue. The term bounded, means that the size of the Queue is fixed and cannot be changed. Any attempt to put element/elements into a full queue will lead to blocking operation. Similarly, 7 min read ConcurrentLinkedDeque Spliterator() method in Java with Examples The spliterator() method of ConcurrentLinkedDeque returns a Spliterator on the elements of ConcurrentLinkedDeque. The returned iterator is weakly consistent. Spliterator can be used with Streams in Java 8. Spliterator can traverse elements individually and in bulk too. Syntax: public Spliterator spl 2 min read Java Program to Implement LinkedList API Linked List is a part of the Collection framework That is present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure in which the elements are not stored in contiguous locations and every element is a separate object with a data pa 10 min read Java Program to Implement PriorityQueue API A PriorityQueue is a linear data structure in which the elements are ordered according to their natural ordering or by some custom comparator provided at the queue at construction time. In PriorityQueue, the front of the queue points to the least element, and the rear points to the greatest element 4 min read Java Program to Implement SynchronousQueue API SynchronousQueue is a special blocking queue with no internal capacity. It helps in exchange data or information between threads in a thread-safe manner. SynchronousQueue has only 2 supported operations: Both of these are blocking method which means when we want to add a piece of information or data 3 min read Java Program to Implement LinkedTransferQueue API LinkedTransferQueue is a queue that orders elements FIFO (first-in-first-out) with respect to any given producer. The head of the queue is that element that has been on the queue the longest time for some producer. The tail of the queue is that element that has been on the queue the shortest time fo 5 min read Like