Java Program to Implement LinkedBlockingDeque API

This Java program Implements LinkedBlockigngDeque API.The optional capacity bound constructor argument serves as a way to prevent excessive expansion. The capacity, if unspecified, is equal to Integer.MAX_VALUE. Linked nodes are dynamically created upon each insertion unless this would bring the deque above capacity.

Here is the source code of the Java Program to Implement LinkedBlockingDeque 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.LinkedBlockingDeque;
  4. import java.util.concurrent.TimeUnit;
  5.  
  6. public class LinkedBlockingDequeImpl<E>
  7. {
  8.     private LinkedBlockingDeque<E> linkedBlockingDeque;
  9.  
  10.     /** Creates a LinkedBlockingDeque with a capacity of Integer.MAX_VALUE. **/
  11.     public LinkedBlockingDequeImpl()
  12.     {
  13.         linkedBlockingDeque = new LinkedBlockingDeque<E>();
  14.     }
  15.  
  16.     /**
  17.      * Creates a LinkedBlockingDeque 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 LinkedBlockingDequeImpl(Collection<? extends E> c)
  22.     {  
  23.         linkedBlockingDeque = new LinkedBlockingDeque<E>(c);
  24.     }
  25.  
  26.     /** Creates a LinkedBlockingDeque with the given (fixed) capacity. **/
  27.     public LinkedBlockingDequeImpl(int capacity)
  28.     {
  29.         linkedBlockingDeque = new LinkedBlockingDeque<E>(capacity);
  30.     }
  31.  
  32.     /** Atomically removes all of the elements from this deque. **/
  33.     public void clear()
  34.     {
  35.         linkedBlockingDeque.clear();
  36.     }
  37.  
  38.     /** Returns true if this deque contains the specified element. **/
  39.     public boolean contains(Object o)
  40.     {
  41.         return linkedBlockingDeque.contains(o);
  42.     }
  43.  
  44.     /**
  45.      * Removes all available elements from this deque and adds them to the given
  46.      * collection.
  47.      **/
  48.     public int drainTo(Collection<? super E> c)
  49.     {
  50.         return linkedBlockingDeque.drainTo(c);
  51.     }
  52.  
  53.     /**
  54.      * Removes at most the given number of available elements from this deque
  55.      * and adds them to the given collection.
  56.      **/
  57.     public int drainTo(Collection<? super E> c, int maxElements)
  58.     {
  59.         return linkedBlockingDeque.drainTo(c, maxElements);
  60.     }
  61.  
  62.     /** Returns an iterator over the elements in this deque in proper sequence. **/
  63.     public Iterator<E> iterator()
  64.     {
  65.         return linkedBlockingDeque.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 deque is full.
  72.      **/
  73.     public boolean offer(E e)
  74.     {
  75.         return linkedBlockingDeque.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 deque is
  81.      * full.
  82.      **/
  83.     public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
  84.     {
  85.         return linkedBlockingDeque.offer(e, timeout, unit);
  86.     }
  87.  
  88.     /**
  89.      * Retrieves, but does not remove, the head of this deque, or returns null
  90.      * if this queue is empty.
  91.      **/
  92.     public E peek()
  93.     {
  94.         return linkedBlockingDeque.peek();
  95.     }
  96.  
  97.     /**
  98.      * Retrieves and removes the head of this deque, or returns null if this
  99.      * queue is empty.
  100.      **/
  101.     public E poll()
  102.     {
  103.         return linkedBlockingDeque.poll();
  104.     }
  105.  
  106.     /**
  107.      * Retrieves and removes the head of this deque, 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 linkedBlockingDeque.poll(timeout, unit);
  113.     }
  114.  
  115.     /**
  116.      * Inserts the specified element at the tail of this deque, waiting for
  117.      * space to become available if the queue is full.
  118.      **/
  119.     public void put(E e) throws InterruptedException
  120.     {
  121.         linkedBlockingDeque.put(e);
  122.     }
  123.  
  124.     /**
  125.      * Returns the number of additional elements that this deque can ideally (in
  126.      * the absence of memory or resource constraints) accept without blocking.
  127.      **/
  128.     public int remainingCapacity()
  129.     {
  130.         return linkedBlockingDeque.remainingCapacity();
  131.     }
  132.  
  133.     /**
  134.      * Removes a single instance of the specified element from this deque, if it
  135.      * is present.
  136.      **/
  137.     public boolean remove(Object o)
  138.     {
  139.         return linkedBlockingDeque.remove(o);
  140.     }
  141.  
  142. .   /** Returns the number of elements in this deque. **/
  143.     public int size()
  144.     {
  145.         return linkedBlockingDeque.size();
  146.     }
  147.  
  148.     /**
  149.      * Retrieves and removes the head of this deque, waiting if necessary until
  150.      * an element becomes available
  151. .    **/
  152.     public E take() throws InterruptedException 
  153.     {
  154.         return linkedBlockingDeque.take();
  155.     }
  156.  
  157.     /**
  158.      * Returns an array containing all of the elements in this deque, in proper
  159.      * sequence.
  160.      **/
  161.     public Object[] toArray()
  162.     {
  163.         return linkedBlockingDeque.toArray();
  164.     }
  165.  
  166.     /**
  167.      * Returns an array containing all of the elements in this deque, 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 linkedBlockingDeque.toArray(a);
  174.     }
  175.  
  176.     /** Returns a string representation of this collection. **/
  177.     public String toString()
  178.     {
  179.         return linkedBlockingDeque.toString();
  180.     }
  181.  
  182.     /** Inserts the specified element at the front of this deque. **/
  183.     public void addFirst(E e)
  184.     {
  185.         linkedBlockingDeque.addFirst(e);
  186.     }
  187.  
  188.     /** Inserts the specified element at the end of this deque. **/
  189.     public void addLast(E e)
  190.     {
  191.         linkedBlockingDeque.addLast(e);
  192.     }
  193.  
  194.     /** Retrieves, but does not remove, the first element of this deque. **/
  195.     public void getFirst()
  196.     {
  197.         linkedBlockingDeque.getFirst();
  198.     }
  199.  
  200.     /** Retrieves, but does not remove, the last element of this deque. **/
  201.     public void getLast()
  202.     {
  203.         linkedBlockingDeque.getLast();
  204.     }
  205.  
  206.     /** Inserts the specified element at the front of this deque. **/
  207.     public boolean offerFirst(E e)
  208.     {
  209.         return linkedBlockingDeque.offerFirst(e);
  210.     }
  211.  
  212.     /** Inserts the specified element at the end of this deque. **/
  213.     public boolean offerLast(E e)
  214.     {
  215.         return linkedBlockingDeque.offerLast(e);
  216.     }
  217.  
  218.     /**
  219.      * Retrieves, but does not remove, the first element of this deque, or
  220.      * returns null if this deque is empty.
  221.      **/
  222.     public E peekFirst()
  223.     {
  224.         return linkedBlockingDeque.peekFirst();
  225.     }
  226.  
  227.     /**
  228.      * Retrieves, but does not remove, the last element of this deque, or
  229.      * returns null if this deque is empty.
  230.      **/
  231.     public E peekLast()
  232.     {
  233.         return linkedBlockingDeque.peekLast();
  234.     }
  235.  
  236.     public static void main(String... arg)
  237.     {
  238.         LinkedBlockingDequeImpl<Integer> linkedBlockingDeque = new LinkedBlockingDequeImpl<Integer>();
  239.         try
  240.         {
  241.             linkedBlockingDeque.put(100);
  242.             linkedBlockingDeque.put(200);
  243.             linkedBlockingDeque.put(300);
  244.         } catch (InterruptedException e)
  245.         {
  246.             e.printStackTrace();
  247.         }
  248.         System.out.println("the elements of the linkedBlockingDeque is ");
  249.         Iterator<Integer> itr = linkedBlockingDeque.iterator();
  250.         while (itr.hasNext())
  251.         {
  252.             System.out.print(itr.next() + "\t");
  253.         }
  254.         System.out.println();
  255.         linkedBlockingDeque.offer(600);
  256.         linkedBlockingDeque.offer(700);
  257.         System.out.println("the peak element of the linkedBlockingDeque is(by peeking) "
  258.             + linkedBlockingDeque.peek());
  259.         System.out.println("the peak element of the linkedBlockingDeque is(by polling) "
  260.             + linkedBlockingDeque.poll());
  261.         System.out.println("the remaining capcity is "+ linkedBlockingDeque.remainingCapacity());
  262.         System.out.println("element 300 removed " + linkedBlockingDeque.remove(300));
  263.         System.out.println("the linkedBlockingDeque contains 400 :" + linkedBlockingDeque.contains(400));
  264.         System.out.println("the linkedBlockingDeque contains 100 :" + linkedBlockingDeque.contains(100));
  265.         System.out.println("the size of the linkedBlockingDeque is "+ linkedBlockingDeque.size());
  266.         System.out.println(linkedBlockingDeque);
  267.     }
  268. }

$ javac LinkedBlockingDequeImpl.java
$ java LinkedBlockingDequeImpl
the elements of the linkedBlockingDeque is 
100	200	300	
the peak element of the linkedBlockingDeque is(by peeking) 100
the peak element of the linkedBlockingDeque is(by polling) 100
the remaining capcity is 2147483643
element 300 removed true
the linkedBlockingDeque contains 400 :false
the linkedBlockingDeque contains 600 :true
the size of the linkedBlockingDeque 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.