ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array.
- ArrayBlockingQueue class is a member of the Java Collections Framework.
- Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.
- The queue also follows FIFO (first-in-first-out) rule for storing and removing elements from the queue.
- If you try to put an element into a full queue or to take an element from an empty queue then the queue will block you.
There are two types of poll() method depending upon no of parameter passed.
- The poll() method retrieves and removes element from head of this queue.If queue is empty then method will return null.
Syntax:public E poll()
Return Value: The method returns the element from the head of this queue, or null if this queue is empty.
Below programs illustrate poll() method of ArrayBlockingQueue.
Program 1:
/**Program Demonstrate poll() method of ArrayBlockingQueue.*/ÂÂimportjava.util.concurrent.ArrayBlockingQueue;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)   Â{       Â// define capacity of ArrayBlockingQueue       Âintcapacity =5;       Â// create object of ArrayBlockingQueue       ÂArrayBlockingQueue<Integer> queue =new              ÂArrayBlockingQueue<Integer>(capacity);       Â// Add elements to ArrayBlockingQueue       Âqueue.offer(423);       Âqueue.offer(233);       Âqueue.offer(356);       Â// print elements       ÂSystem.out.println("Queue Contains"+ queue);       Â// try to poll elements       ÂSystem.out.println("Removing From head: "+                                      Âqueue.poll());       ÂSystem.out.println("Queue Contains"+ queue);       ÂSystem.out.println("Removing From head: "+                                      Âqueue.poll());       ÂSystem.out.println("Queue Contains"+ queue);       ÂSystem.out.println("Removing From head: "+                                      Âqueue.poll());       ÂSystem.out.println("Queue Contains"+ queue);       ÂSystem.out.println("Removing From head: "+                                      Âqueue.poll());       ÂSystem.out.println("Queue Contains"+ queue);   Â}}Output:Queue Contains[423, 233, 356] Removing From head: 423 Queue Contains[233, 356] Removing From head: 233 Queue Contains[356] Removing From head: 356 Queue Contains[] Removing From head: null Queue Contains[]
Program 2:
/** Program Demonstrate poll() method of ArrayBlockingQueue.*/ÂÂimportjava.util.concurrent.ArrayBlockingQueue;ÂÂpublicclassGFG {   Â// Create a User Object with name and age as an attribute   ÂpublicclassUser {       ÂpublicString name;       ÂpublicString age;       ÂUser(String name, String age)       Â{           Âthis.name = name;           Âthis.age = age;       Â}   Â}   Â// Main Method   Âpublicstaticvoidmain(String[] args)   Â{       ÂGFG gfg =newGFG();       Âgfg.pollMethodExample();   Â}   Â// Method to give example of poll function   ÂpublicvoidpollMethodExample()   Â{       Â// Define the capacity of ArrayBlockingQueue       Âintcapacity =5;       Â// Create object of ArrayBlockingQueue       ÂArrayBlockingQueue<User> queue =              ÂnewArrayBlockingQueue<User>(capacity);       Â// Create user objects       ÂUser user1 =newUser("Aman","24");       ÂUser user3 =newUser("Sanjeet","25");       Â// Add Objects to ArrayBlockingQueue       Âqueue.offer(user1);       Âqueue.offer(user3);       Â// Poll users from queue       ÂUser user = queue.poll();       ÂSystem.out.println("removing user having name = "                                           Â+ user.name);       Âuser = queue.poll();       ÂSystem.out.println("removing user having name = "                                           Â+ user.name);       Â// Now queue is empty       Â// Try to remove it will return null       Âuser = queue.poll();       ÂSystem.out.println("removing user having name = "                                                Â+ user);   Â}}Output:removing user having name = Aman removing user having name = Sanjeet removing user having name = null
- The poll(long timeout, TimeUnit unit) method retrieves and removes element from head of this queue. If the queue is empty then it will, wait till a specified time for an element to become available.
Syntax:public E poll(long timeout, TimeUnit unit) throws InterruptedException
Parameters: The method takes two parameters:
- timeout (long) – how long to wait before giving up, in units of unit.
- unit (TimeUnit)- a TimeUnit determining how to interpret the timeout parameter.
Return Value: The method returns the head of this queue, or null if the specified waiting time elapses before an element is available.
Exception: The method throws InterruptedException if interrupted while waiting.Below program illustrates poll(long timeout, TimeUnit unit)method of ArrayBlockingQueue.
/** Program Demonstrate offer(E e, long timeout, TimeUnit unit)* method of ArrayBlockingQueue.*/ÂÂimportjava.util.concurrent.ArrayBlockingQueue;importjava.util.concurrent.TimeUnit;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)                      ÂthrowsInterruptedException   Â{       Â// Define capacity of ArrayBlockingQueue       Âintcapacity =5;       Â// Create object of ArrayBlockingQueue       ÂArrayBlockingQueue<Integer> queue =           ÂnewArrayBlockingQueue<Integer>(capacity);       Â// Add elements to ArrayBlockingQueue       Âqueue.offer(423);       Âqueue.offer(233);       Âqueue.offer(356);       Â// Print elements       ÂSystem.out.println("Queue Contains"+ queue);       Â// Try to poll elements       ÂSystem.out.println("Removing From head: "                 Â+ queue.poll(10, TimeUnit.SECONDS));       ÂSystem.out.println("Queue Contains"+ queue);       ÂSystem.out.println("Removing From head: "                 Â+ queue.poll(10, TimeUnit.SECONDS));       ÂSystem.out.println("Queue Contains"+ queue);       ÂSystem.out.println("Removing From head: "+                   Âqueue.poll(10, TimeUnit.SECONDS));       ÂSystem.out.println("Queue Contains"+ queue);       ÂSystem.out.println("Removing From head: "+                  Âqueue.poll(10, TimeUnit.SECONDS));   Â}}Output:Queue Contains[423, 233, 356] Removing From head: 423 Queue Contains[233, 356] Removing From head: 233 Queue Contains[356] Removing From head: 356 Queue Contains[] Removing From head: null
Reference:
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#poll()

… [Trackback]
[…] Find More Information here on that Topic: geeksforgeeks.org/arrayblockingqueue-poll-method-in-java/ […]