Java Program to Implement ConcurrentLinkedQueue API

This Java program is to Implement ConcurrentLinkedQueue API.An unbounded thread-safe queue based on linked nodes. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. A ConcurrentLinkedQueue is an appropriate choice when many threads will share access to a common collection. Like most other concurrent collection implementations, this class does not permit the use of null elements.

Here is the source code of the Java program to Implement ConcurrentLinkedQueue API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
 
public class ConcurrentLinkedQueueImpl<E>
{
    private ConcurrentLinkedQueue<E> concurrentLinkedQueue;
 
    /** Creates a ConcurrentLinkedQueue that is initially empty. **/
    public ConcurrentLinkedQueueImpl()
    {
        concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
    }
 
    /**
     * Creates a ConcurrentLinkedQueue initially containing the elements of the
     * given collection, added in traversal order of the collection's iterator.
     **/
    public ConcurrentLinkedQueueImpl(Collection<? extends E> c)
    {
        concurrentLinkedQueue = new ConcurrentLinkedQueue<>(c);
    }
 
    /** Inserts the specified element at the tail of this queue. **/
    public boolean add(E e)
    {
        return concurrentLinkedQueue.add(e);
    }
 
    /** Returns true if this queue contains the specified element. **/
    public boolean contains(Object o)
    {
        return concurrentLinkedQueue.contains(o);
    }
 
    /** Returns an iterator over the elements in this queue in proper sequence. **/
    public Iterator<E> iterator()
    {
        return concurrentLinkedQueue.iterator();
    }
 
    /** Inserts the specified element at the tail of this queue. **/
    public boolean offer(E e)
    {
        return concurrentLinkedQueue.offer(e);
    }
 
    /**
     * Retrieves, but does not remove, the head of this 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 number of elements in this queue. **/
    public int size()
    {
        return concurrentLinkedQueue.size();
    }
 
    /**
     * Returns an array containing all of the elements in this queue, in proper
     * sequence.
     **/
    public Object[] toArray()
    {
        return concurrentLinkedQueue.toArray();
    }
 
    /**
     * Returns an array containing all of the elements in this queue, in proper
     * sequence; the runtime type of the returned array is that of the specified
     * array.
     **/
    public <T> T[] toArray(T[] a)
    {
        return concurrentLinkedQueue.toArray(a);
    }
 
    public static void main(String... arg)
    {
        ConcurrentLinkedQueueImpl<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueueImpl<Integer>();
        concurrentLinkedQueue.add(100);
        concurrentLinkedQueue.add(200);
        concurrentLinkedQueue.add(300);
        concurrentLinkedQueue.add(400);
        concurrentLinkedQueue.add(500);
 
        System.out.println("the elements of the arrayblockingqueue is ");
        Iterator<Integer> itr = concurrentLinkedQueue.iterator();
        while (itr.hasNext())
        {
            System.out.print(itr.next() + "\t");
        }
        System.out.println();
        concurrentLinkedQueue.offer(600);
        concurrentLinkedQueue.offer(700);
        System.out.println("the peak element of the concurrentLinkedQueue is(by peeking) "
            + concurrentLinkedQueue.peek());
        System.out.println("the peak element of the concurrentLinkedQueue is(by polling) "
            + concurrentLinkedQueue.poll());
        System.out.println("element 300 removed " + concurrentLinkedQueue.remove(300));
        System.out.println("the concurrentLinkedQueue contains 400 :"
            + concurrentLinkedQueue.contains(400));
        System.out.println("the hash concurrentLinkedQueue contains 100 :"
            + concurrentLinkedQueue.contains(100));
        System.out.println("the size of the concurrentLinkedQueue is "
           + concurrentLinkedQueue.size());
    }
}
$ javac ConcurrentLinkedQueueImpl.java
$ java ConcurrentLinkedQueueImpl
the elements of the arrayblockingqueue is 
100	200	300	400	500	
the peak element of the concurrentLinkedQueue is(by peeking) 100
the peak element of the concurrentLinkedQueue is(by polling) 100
element 300 removed true
the concurrentLinkedQueue contains 400 :true
the hash concurrentLinkedQueue contains 100 :false
the size of the concurrentLinkedQueue is 5

Related posts:

Hướng dẫn Java Design Pattern – Flyweight
Constructor Injection in Spring with Lombok
Java Program to Implement Rope
Hướng dẫn Java Design Pattern – Visitor
Java Program to Implement Variable length array
Java – Rename or Move a File
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Lớp LinkedHashMap trong Java
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java Program to Perform Addition Operation Using Bitwise Operators
Java Program to Use rand and srand Functions
Tạo số và chuỗi ngẫu nhiên trong Java
How to Break from Java Stream forEach
Deploy a Spring Boot WAR into a Tomcat Server
Java Program to Implement Find all Cross Edges in a Graph
Lớp lồng nhau trong java (Java inner class)
Chuyển đổi giữa các kiểu dữ liệu trong Java
Java Program to Implement HashTable API
Hướng dẫn Java Design Pattern – Interpreter
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
A Guide to the ResourceBundle
Iterating over Enum Values in Java
Java Program to Solve Knapsack Problem Using Dynamic Programming
Hướng dẫn Java Design Pattern – Builder
Java Program to Implement Suffix Array
Limiting Query Results with JPA and Spring Data JPA
Bootstrapping Hibernate 5 with Spring
@DynamicUpdate with Spring Data JPA
Adding a Newline Character to a String in Java
Java Program to Encode a Message Using Playfair Cipher
Java Program to Find Nearest Neighbor Using Linear Search