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:

Convert Hex to ASCII in Java
Generate Spring Boot REST Client with Swagger
Java Program to Implement Caesar Cypher
Java Program to Implement Quick sort
Spring Boot - Tomcat Deployment
How to use the Spring FactoryBean?
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Integer Constant Pool trong Java
Creating Docker Images with Spring Boot
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Using a Mutex Object in Java
Java Program to Solve TSP Using Minimum Spanning Trees
Java Program to Implement Sorted Vector
Quick Guide to the Java StringTokenizer
Quick Guide to Spring Bean Scopes
Java Program to Implement Fermat Factorization Algorithm
Java Program to Implement Binomial Heap
Hướng dẫn sử dụng luồng vào ra ký tự trong Java
Java Program to Implement Insertion Sort
Automatic Property Expansion with Spring Boot
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Java Program to Implement Coppersmith Freivald’s Algorithm
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
A Guide to System.exit()
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Java Program to Evaluate an Expression using Stacks
wait() and notify() Methods in Java
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Program to Check for balanced parenthesis by using Stacks
Iterating over Enum Values in Java
Java Program to Implement Rope