PriorityBlockingQueue poll() method in Java
Last Updated :
26 Nov, 2018
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.
import java.util.concurrent.PriorityBlockingQueue;
public class GFG {
public static void main(String[] args)
{
int capacityOfQueue = 5 ;
PriorityBlockingQueue<Integer> PrioQueue
= new PriorityBlockingQueue<Integer>(capacityOfQueue);
PrioQueue.offer( 35658786 );
PrioQueue.offer( 5278367 );
PrioQueue.offer( 74381793 );
PrioQueue.offer( 87625142 );
int removedItem = PrioQueue.poll();
System.out.println( "Removed Element: " + removedItem);
System.out.println( "Now Queue Contains:" );
System.out.println(PrioQueue.toString());
}
}
|
Output:
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.
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacityOfQueue = 5 ;
PriorityBlockingQueue<String> names
= new PriorityBlockingQueue<String>(capacityOfQueue);
names.offer( "Joyita" );
names.offer( "Priyanka" );
String removedName1 = names.poll();
String removedName2 = names.poll();
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());
String removedName3 = names.poll();
System.out.println( "Removed Name 3: " + removedName3);
}
}
|
Output:
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.
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
int capacityOfQueue = 5 ;
PriorityBlockingQueue<Integer> PrioQueue
= new PriorityBlockingQueue<Integer>(capacityOfQueue);
PrioQueue.offer( 35658786 );
PrioQueue.offer( 5278367 );
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);
}
}
|
Output:
Removed Number: 5278367
List Contains[35658786]
Removed Number: 35658786
List Contains[]
Removed Number: null
List Contains[]
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...