Java Program to Implement ArrayBlockingQueue API

This Java program is to Implement ArrayBlockingQueue API.A bounded blocking queue backed by an array. 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.

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

$ javac ArrayBlockingQueueImpl.java
$ java ArrayBlockingQueueImpl
the elements of the arrayblockingqueue is 
100	200	300	400	500	
the peak element of the arrayblockingqueue is(by peeking) 100
the peak element of the arrayblockingqueue is(by polling) 100
the remaining capacity is 4
element 300 removed true
the arrayblockingqueue contains 400 :true
the hash arrayblockingqueue contains 100 :false
the size of the arrayblocingqueue is 5
[200, 400, 500, 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.