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.

  1. import java.util.Collection;
  2. import java.util.Iterator;
  3. import java.util.concurrent.ConcurrentLinkedQueue;
  4.  
  5. public class ConcurrentLinkedQueueImpl<E>
  6. {
  7.     private ConcurrentLinkedQueue<E> concurrentLinkedQueue;
  8.  
  9.     /** Creates a ConcurrentLinkedQueue that is initially empty. **/
  10.     public ConcurrentLinkedQueueImpl()
  11.     {
  12.         concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
  13.     }
  14.  
  15.     /**
  16.      * Creates a ConcurrentLinkedQueue initially containing the elements of the
  17.      * given collection, added in traversal order of the collection's iterator.
  18.      **/
  19.     public ConcurrentLinkedQueueImpl(Collection<? extends E> c)
  20.     {
  21.         concurrentLinkedQueue = new ConcurrentLinkedQueue<>(c);
  22.     }
  23.  
  24.     /** Inserts the specified element at the tail of this queue. **/
  25.     public boolean add(E e)
  26.     {
  27.         return concurrentLinkedQueue.add(e);
  28.     }
  29.  
  30.     /** Returns true if this queue contains the specified element. **/
  31.     public boolean contains(Object o)
  32.     {
  33.         return concurrentLinkedQueue.contains(o);
  34.     }
  35.  
  36.     /** Returns an iterator over the elements in this queue in proper sequence. **/
  37.     public Iterator<E> iterator()
  38.     {
  39.         return concurrentLinkedQueue.iterator();
  40.     }
  41.  
  42.     /** Inserts the specified element at the tail of this queue. **/
  43.     public boolean offer(E e)
  44.     {
  45.         return concurrentLinkedQueue.offer(e);
  46.     }
  47.  
  48.     /**
  49.      * Retrieves, but does not remove, the head of this queue, or returns null
  50.      * if this queue is empty.
  51.      **/
  52.     public E peek()
  53.     {
  54.         return concurrentLinkedQueue.peek();
  55.     }
  56.  
  57.     /**
  58.      * Retrieves and removes the head of this queue, or returns null if this
  59.      * queue is empty.
  60.      **/
  61.     public E poll() 
  62.     {
  63.         return concurrentLinkedQueue.poll();
  64.     }
  65.  
  66.     /**
  67.      * Removes a single instance of the specified element from this queue, if it
  68.      * is present.
  69.      **/
  70.     public boolean remove(Object o)
  71.     {
  72.         return concurrentLinkedQueue.remove(o);
  73.     }
  74.  
  75.     /** Returns the number of elements in this queue. **/
  76.     public int size()
  77.     {
  78.         return concurrentLinkedQueue.size();
  79.     }
  80.  
  81.     /**
  82.      * Returns an array containing all of the elements in this queue, in proper
  83.      * sequence.
  84.      **/
  85.     public Object[] toArray()
  86.     {
  87.         return concurrentLinkedQueue.toArray();
  88.     }
  89.  
  90.     /**
  91.      * Returns an array containing all of the elements in this queue, in proper
  92.      * sequence; the runtime type of the returned array is that of the specified
  93.      * array.
  94.      **/
  95.     public <T> T[] toArray(T[] a)
  96.     {
  97.         return concurrentLinkedQueue.toArray(a);
  98.     }
  99.  
  100.     public static void main(String... arg)
  101.     {
  102.         ConcurrentLinkedQueueImpl<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueueImpl<Integer>();
  103.         concurrentLinkedQueue.add(100);
  104.         concurrentLinkedQueue.add(200);
  105.         concurrentLinkedQueue.add(300);
  106.         concurrentLinkedQueue.add(400);
  107.         concurrentLinkedQueue.add(500);
  108.  
  109.         System.out.println("the elements of the arrayblockingqueue is ");
  110.         Iterator<Integer> itr = concurrentLinkedQueue.iterator();
  111.         while (itr.hasNext())
  112.         {
  113.             System.out.print(itr.next() + "\t");
  114.         }
  115.         System.out.println();
  116.         concurrentLinkedQueue.offer(600);
  117.         concurrentLinkedQueue.offer(700);
  118.         System.out.println("the peak element of the concurrentLinkedQueue is(by peeking) "
  119.             + concurrentLinkedQueue.peek());
  120.         System.out.println("the peak element of the concurrentLinkedQueue is(by polling) "
  121.             + concurrentLinkedQueue.poll());
  122.         System.out.println("element 300 removed " + concurrentLinkedQueue.remove(300));
  123.         System.out.println("the concurrentLinkedQueue contains 400 :"
  124.             + concurrentLinkedQueue.contains(400));
  125.         System.out.println("the hash concurrentLinkedQueue contains 100 :"
  126.             + concurrentLinkedQueue.contains(100));
  127.         System.out.println("the size of the concurrentLinkedQueue is "
  128.            + concurrentLinkedQueue.size());
  129.     }
  130. }

$ 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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.