Java Program to Implement PriorityBlockingQueue API

This Java program Implements PriorityBlockingQueue API.An unbounded blocking queue that uses the same ordering rules as class PriorityQueue and supplies blocking retrieval operations. While this queue is logically unbounded, attempted additions may fail due to resource exhaustion (causing OutOfMemoryError). This class does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects.

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

$ javac PriorityBlockingQueueImpl.java
$ java PriorityBlockingQueueImpl
the elements of the PriorityBlockingQueue is 
-400	200	600	300	240	
the peak element of the PriorityBlockingQueue is(by peeking) -400
the peak element of the PriorityBlockingQueue is(by polling) -400
the remaining capcity is 2147483647
element 300 removed true
the PriorityBlockingQueue contains 400 :false
the PriorityBlockingQueue contains 100 :false
the size of the PriorityBlockingQueue is 5
[200, 240, 600, 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.