Java Program to Implement LinkedTransferQueue API

This Java program Implements LinkedTransferQueue API.An unbounded TransferQueue based on linked nodes. This queue orders elements FIFO (first-in-first-out) with respect to any given producer. The head of the queue is that element that has been on the queue the longest time for some producer. The tail of the queue is that element that has been on the queue the shortest time for some producer.

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

$ javac LinkedTransferQueueImpl.java
$ java LinkedTransferQueueImpl
the elements of the linkedTransferQueue is 
100	200	300	400	500	
the peak element of the linkedTransferQueue is(by peeking) 100
the peak element of the linkedTransferQueue is(by polling) 100
the remaining capcity is 2147483647
the remaining consumer waiting count : 0
element 300 removed true
the linkedTransferQueue contains 400 :true
the linkedTransferQueue contains 100 :false
the size of the linkedTransferQueue 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.