1. poll() Method
The poll() method of PriorityBlockingQueue retrieves and removes element from head of this PriorityBlockingQueue. This method returns the element it removes from PriorityBlockingQueue but when the queue is empty then method will return null.
Syntax:
public E poll()
Returns: This method returns the element from the head of this PriorityBlockingQueue, or null if this queue is empty.
Below programs illustrate poll() method of PriorityBlockingQueue:
Example 1: Program to demonstrate poll() method on PriorityBlockingQueue to remove a element from a list of numbers.
// Java Program Demonstrate poll() // method of PriorityBlockingQueue import java.util.concurrent.PriorityBlockingQueue; public class GFG { public static void main(String[] args) { // define capacity of PriorityBlockingQueue int capacityOfQueue = 5 ; // create object of PriorityBlockingQueue PriorityBlockingQueue<Integer> PrioQueue = new PriorityBlockingQueue<Integer>(capacityOfQueue); // Add numbers to PriorityBlockingQueue PrioQueue.offer( 35658786 ); PrioQueue.offer( 5278367 ); PrioQueue.offer( 74381793 ); PrioQueue.offer( 87625142 ); // remove numbers from head using poll() // and print removed number int removedItem = PrioQueue.poll(); // print details System.out.println( "Removed Element: " + removedItem); System.out.println( "Now Queue Contains:" ); System.out.println(PrioQueue.toString()); } } |
Removed Element: 5278367 Now Queue Contains: [35658786, 87625142, 74381793]
Example 2: Program to demonstrate poll() method to remove String from a list of String values and return null if the list is empty.
// Java Program Demonstrate poll() // method of PriorityBlockingQueue import java.util.concurrent.PriorityBlockingQueue; import java.util.*; public class GFG { public static void main(String[] args) { // define capacity of PriorityBlockingQueue int capacityOfQueue = 5 ; // create object of PriorityBlockingQueue which contains // name of students PriorityBlockingQueue<String> names = new PriorityBlockingQueue<String>(capacityOfQueue); // Add names of students of girls college names.offer( "Joyita" ); names.offer( "Priyanka" ); // remove two names from list of names // and print removed name String removedName1 = names.poll(); String removedName2 = names.poll(); // print details System.out.println( "Removed Name 1: " + removedName1); System.out.println( "Removed Name 2: " + removedName2); System.out.println( "Now Queue Contains:" ); System.out.println(names.toString()); // try to remove from empty PriorityBlockingQueue String removedName3 = names.poll(); System.out.println( "Removed Name 3: " + removedName3); } } |
Removed Name 1: Joyita Removed Name 2: Priyanka Now Queue Contains: [] Removed Name 3: null
2. poll(long timeout, TimeUnit unit) Method
The poll(long timeout, TimeUnit unit) method of PriorityBlockingQueue retrieves and removes element from head of this PriorityBlockingQueue.If the PriorityBlockingQueue is empty then it will, wait till a specified time for an element to become available.Waiting time and unit of time is given as parameters to method.
Syntax:
public E poll(long timeout, TimeUnit unit) throws InterruptedException
Parameter:
This method accepts 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.
Returns: This method returns the element from the head of this PriorityBlockingQueue, or null if the specified waiting time elapses before an element is available.
Exception: This method throws only one Exception InterruptedException – if interrupted while waiting
Below programs illustrate poll(long timeout, TimeUnit unit) method of PriorityBlockingQueue:
Example 1: Program to demonstrate poll(long timeout, TimeUnit unit) method on PriorityBlockingQueue to remove a element from a list of numbers.
// Java Program Demonstrate poll(long timeout, TimeUnit unit) // method of PriorityBlockingQueue import java.util.concurrent.PriorityBlockingQueue; import java.util.concurrent.TimeUnit; public class GFG { public static void main(String[] args) throws InterruptedException { // define capacity of PriorityBlockingQueue int capacityOfQueue = 5 ; // create object of PriorityBlockingQueue PriorityBlockingQueue<Integer> PrioQueue = new PriorityBlockingQueue<Integer>(capacityOfQueue); // Add numbers to PriorityBlockingQueue PrioQueue.offer( 35658786 ); PrioQueue.offer( 5278367 ); // Try to poll elements from PriorityBlockingQueue System.out.println( "Removed Number: " + PrioQueue.poll( 10 , TimeUnit.SECONDS)); System.out.println( "List Contains" + PrioQueue); System.out.println( "Removed Number: " + PrioQueue.poll( 10 , TimeUnit.SECONDS)); System.out.println( "List Contains" + PrioQueue); System.out.println( "Removed Number: " + PrioQueue.poll( 10 , TimeUnit.SECONDS)); System.out.println( "List Contains" + PrioQueue); } } |
Removed Number: 5278367 List Contains[35658786] Removed Number: 35658786 List Contains[] Removed Number: null List Contains[]
Reference: