Java Program to Implement LinkedBlockingQueue API

This Java program Implements LinkedBlockingQueue API.An optionally-bounded blocking 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. Linked queues typically have higher throughput than array-based queues but less predictable performance in most concurrent applications.

Here is the source code of the Java Program to Implement LinkedBlockingQueue. 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.LinkedBlockingQueue;
  4. import java.util.concurrent.TimeUnit;
  5.  
  6. public class LinkedBlockingQueueImpl<E>
  7. {
  8.     private LinkedBlockingQueue<E> linkedBlockingQueue;
  9.  
  10.     /** Creates a LinkedBlockingQueue with a capacity of Integer.MAX_VALUE. **/
  11.     public LinkedBlockingQueueImpl()
  12.     {
  13.         linkedBlockingQueue = new LinkedBlockingQueue<E>();
  14.     }
  15.  
  16.     /**
  17.      * Creates a LinkedBlockingQueue with a capacity of Integer.MAX_VALUE,
  18.      * initially containing the elements of the given collection, added in
  19.      * traversal order of the collection's iterator.
  20.      **/
  21.     public LinkedBlockingQueueImpl(Collection<? extends E> c)
  22.     {
  23.         linkedBlockingQueue = new LinkedBlockingQueue<E>(c);
  24.     }
  25.  
  26.     /** Creates a LinkedBlockingQueue with the given (fixed) capacity. **/
  27.     public LinkedBlockingQueueImpl(int capacity)
  28.     {
  29.         linkedBlockingQueue = new LinkedBlockingQueue<E>(capacity);
  30.     }
  31.  
  32.     /** Atomically removes all of the elements from this queue. **/
  33.     void clear()
  34.     {
  35.         linkedBlockingQueue.clear();
  36.     }
  37.  
  38.     /** Returns true if this queue contains the specified element. **/
  39.     public boolean contains(Object o)
  40.     {
  41.         return linkedBlockingQueue.contains(o);
  42.     }
  43.  
  44.     /**
  45.      * Removes all available elements from this queue and adds them to the given
  46.      * collection.
  47.      **/
  48.     public int drainTo(Collection<? super E> c)
  49.     {
  50.         return linkedBlockingQueue.drainTo(c);
  51.     }
  52.  
  53.     /**
  54.      * Removes at most the given number of available elements from this queue
  55.      * and adds them to the given collection.
  56.      **/
  57.     public int drainTo(Collection<? super E> c, int maxElements)
  58.     {
  59.         return linkedBlockingQueue.drainTo(c, maxElements);
  60.     }
  61.  
  62.     /** Returns an iterator over the elements in this queue in proper sequence. **/
  63.     public Iterator<E> iterator()
  64.     {
  65.         return linkedBlockingQueue.iterator();
  66.     }
  67.  
  68.     /**
  69.      * Inserts the specified element at the tail of this queue if it is possible
  70.      * to do so immediately without exceeding the queue's capacity, returning
  71.      * true upon success and false if this queue is full.
  72.      **/
  73.     public boolean offer(E e)
  74.     {
  75.         return linkedBlockingQueue.offer(e);
  76.     }
  77.  
  78.     /**
  79.      * Inserts the specified element at the tail of this queue, waiting up to
  80.      * the specified wait time for space to become available if the queue is
  81.      * full.
  82.      **/
  83.     public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
  84.     {
  85.         return linkedBlockingQueue.offer(e, timeout, unit);
  86.     }
  87.  
  88.     /**
  89.      * Retrieves, but does not remove, the head of this queue, or returns null
  90.      * if this queue is empty.
  91.      **/
  92.     public E peek()
  93.     {
  94.         return linkedBlockingQueue.peek();
  95.     }
  96.  
  97.     /**
  98.      * Retrieves and removes the head of this queue, or returns null if this
  99.      * queue is empty.
  100.      **/
  101.     public E poll()
  102.     {
  103.         return linkedBlockingQueue.poll();
  104.     }
  105.  
  106.     /**
  107.      * Retrieves and removes the head of this queue, waiting up to the specified
  108.      * wait time if necessary for an element to become available.
  109.      **/
  110.     public E poll(long timeout, TimeUnit unit) throws InterruptedException
  111.     {
  112.         return linkedBlockingQueue.poll(timeout, unit);
  113.     }
  114.  
  115.     /**
  116.      * Inserts the specified element at the tail of this queue, waiting for
  117.      * space to become available if the queue is full.
  118.      **/
  119.     public void put(E e) throws InterruptedException
  120.     {
  121.         linkedBlockingQueue.put(e);
  122.     }
  123.  
  124.     /**
  125.      * Returns the number of additional elements that this queue can ideally (in
  126.      * the absence of memory or resource constraints) accept without blocking.
  127.      **/
  128.     public int remainingCapacity()
  129.     {
  130.         return linkedBlockingQueue.remainingCapacity();
  131.     }
  132.  
  133.     /**
  134.      * Removes a single instance of the specified element from this queue, if it
  135.      * is present.
  136.      **/
  137.     public boolean remove(Object o)
  138.     {
  139.         return linkedBlockingQueue.remove(o);
  140.     }
  141.  
  142.     /** Returns the number of elements in this queue. **/
  143.     public int size()
  144.     {
  145.         return linkedBlockingQueue.size();
  146.     }
  147.  
  148.     /**
  149.      * Retrieves and removes the head of this queue, waiting if necessary until
  150.      * an element becomes available
  151.      **/
  152.     public E take() throws InterruptedException
  153.     {
  154.         return linkedBlockingQueue.take();
  155.     }
  156.  
  157.     /**
  158.      * Returns an array containing all of the elements in this queue, in proper
  159.      * sequence.
  160.      **/
  161.     public Object[] toArray()
  162.     {
  163.         return linkedBlockingQueue.toArray();
  164.     }
  165.  
  166.     /**
  167.      * Returns an array containing all of the elements in this queue, in proper
  168.      * sequence; the runtime type of the returned array is that of the specified
  169.      * array.
  170.      **/
  171.     public <T> T[] toArray(T[] a)
  172.     {
  173.         return linkedBlockingQueue.toArray(a);
  174.     }
  175.  
  176.     /** Returns a string representation of this collection. **/
  177.     public String toString()
  178.     {
  179.         return linkedBlockingQueue.toString();
  180.     }
  181.  
  182.     public static void main(String... arg)
  183.     {
  184.         LinkedBlockingQueueImpl<Integer> linkedBlockingQueue = new LinkedBlockingQueueImpl<Integer>();
  185.         try
  186.         {
  187.             linkedBlockingQueue.put(100);
  188.             linkedBlockingQueue.put(200);
  189.             linkedBlockingQueue.put(300);
  190.         } catch (InterruptedException e)
  191.         {
  192.             e.printStackTrace();
  193.         }
  194.         System.out.println("the elements of the linkedBlockingQueue is ");
  195.         Iterator<Integer> itr = linkedBlockingQueue.iterator();
  196.         while (itr.hasNext())
  197.         {
  198.             System.out.print(itr.next() + "\t");
  199.         }
  200.         System.out.println();
  201.         linkedBlockingQueue.offer(600);
  202.         linkedBlockingQueue.offer(700);
  203.         System.out.println("the peak element of the linkedBlockingQueue is(by peeking) " 
  204.             + linkedBlockingQueue.peek());
  205.         System.out.println("the peak element of the linkedBlockingQueue is(by polling) "
  206.             + linkedBlockingQueue.poll());
  207.         System.out.println("the remaining capcity is "
  208.             + linkedBlockingQueue.remainingCapacity());
  209.         System.out.println("element 300 removed "
  210.             + linkedBlockingQueue.remove(300));
  211.         System.out.println("the linkedBlockingQueue contains 400 :"
  212.             + linkedBlockingQueue.contains(400));
  213.         System.out.println("the linkedBlockingQueue contains 100 :"
  214.             + linkedBlockingQueue.contains(100));
  215.         System.out.println("the size of the linkedBlockingQueue is "
  216.             + linkedBlockingQueue.size());
  217.         System.out.println(linkedBlockingQueue);
  218.     }
  219. }

$ javac LinkedBlockingQueueImpl.java
$ java LinkedBlockingQueueImpl
the elements of the linkedBlockingQueue is 
100	200	300	
the peak element of the linkedBlockingQueue is(by peeking) 100
the peak element of the linkedBlockingQueue is(by polling) 100
the remaining capcity is 2147483643
element 300 removed true
the linkedBlockingQueue contains 400 :false
the linkedBlockingQueue contains 100 :false
the size of the linkedBlockingQueue is 3
[200, 600, 700]

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.