ArrayBlockingQueue poll() Method in Java Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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: Java /* *Program Demonstrate poll() method of ArrayBlockingQueue. */ import java.util.concurrent.ArrayBlockingQueue; public class GFG { public static void main(String[] args) { // define capacity of ArrayBlockingQueue int capacity = 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: Java /* * Program Demonstrate poll() method of ArrayBlockingQueue. */ import java.util.concurrent.ArrayBlockingQueue; public class GFG { // Create a User Object with name and age as an attribute public class User { public String name; public String age; User(String name, String age) { this.name = name; this.age = age; } } // Main Method public static void main(String[] args) { GFG gfg = new GFG(); gfg.pollMethodExample(); } // Method to give example of poll function public void pollMethodExample() { // Define the capacity of ArrayBlockingQueue int capacity = 5; // Create object of ArrayBlockingQueue ArrayBlockingQueue<User> queue = new ArrayBlockingQueue<User>(capacity); // Create user objects User user1 = new User("Aman", "24"); User user3 = new User("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. Java /* * Program Demonstrate offer(E e, long timeout, TimeUnit unit) * method of ArrayBlockingQueue. */ import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.TimeUnit; public class GFG { public static void main(String[] args) throws InterruptedException { // Define capacity of ArrayBlockingQueue int capacity = 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(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() Create Quiz Comment A AmanSingh2210 Follow 0 Improve A AmanSingh2210 Follow 0 Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-ArrayBlockingQueue +1 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like