LinkedBlockingQueue poll() method in Java
Last Updated :
26 Nov, 2018
There is two types of poll() method in LinkedBlockingQueue.
poll()
The poll() method of LinkedBlockingQueue returns the head of LinkedBlockingQueue by removing that element from queue. It can be said that this method retrieves and removes element from head of this LinkedBlockingQueue. If queue is empty then poll method returns null.
Syntax:
public E poll()
Return Value: This method retrieves and removes element from head of this LinkedBlockingQueue. If the queue is empty, then it returns null.
Below programs illustrates poll() method of LinkedBlockingQueue class:
Program 1: Removing elements from the LinkedBlockingQueue using poll() method where LinkedBlockingQueue contains list of names.
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
int capacityOfQueue = 4 ;
LinkedBlockingQueue<String> linkedQueue
= new LinkedBlockingQueue<String>(capacityOfQueue);
linkedQueue.add( "Ravi" );
linkedQueue.add( "Suraj" );
linkedQueue.add( "Harsh" );
linkedQueue.add( "Sayan" );
System.out.println( "Items in Queue are " + linkedQueue);
String removedItem1 = linkedQueue.poll();
System.out.println( "Removed Item is " + removedItem1);
System.out.println( "Remaining Items in Queue are "
+ linkedQueue);
String removedItem2 = linkedQueue.poll();
System.out.println( "Removed Item is " + removedItem2);
System.out.println( "Remaining Items in Queue are " + linkedQueue);
}
}
|
Output:
Items in Queue are [Ravi, Suraj, Harsh, Sayan]
Removed Item is Ravi
Remaining Items in Queue are [Suraj, Harsh, Sayan]
Removed Item is Suraj
Remaining Items in Queue are [Harsh, Sayan]
Program 2: Removing elements from LinkedBlockingQueue which contains list of Employees and if queue is empty print null.
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public void pollingMethod()
{
int capacityOfQueue = 5 ;
LinkedBlockingQueue<Employee> linkedQueue
= new LinkedBlockingQueue<Employee>(capacityOfQueue);
Employee emp1 = new Employee( "Ravi" , "Tester" , "39000" );
Employee emp2 = new Employee( "Sanjeet" , "Manager" , "98000" );
linkedQueue.add(emp1);
linkedQueue.add(emp2);
while (linkedQueue.size() != 0 ) {
Employee removedEmp = linkedQueue.poll();
System.out.println( "Removed Item is :" );
System.out.println( "Employee Name - " + removedEmp.name);
System.out.println( "Employee Position - " + removedEmp.position);
System.out.println( "Employee Salary - " + removedEmp.salary);
int size = linkedQueue.size();
System.out.println( "\nSize of list :" + size + "\n" );
}
Employee removedEmp = linkedQueue.poll();
System.out.println( "Removed Item is : " + removedEmp);
}
public class Employee {
public String name;
public String position;
public String salary;
Employee(String name, String position, String salary)
{
this .name = name;
this .position = position;
this .salary = salary;
}
@Override
public String toString()
{
return "Employee [name=" + name + ", position="
+ position + ", salary=" + salary + "]" ;
}
}
public static void main(String[] args)
{
GFG gfg = new GFG();
gfg.pollingMethod();
}
}
|
Output:
Removed Item is :
Employee Name - Ravi
Employee Position - Tester
Employee Salary - 39000
Size of list :1
Removed Item is :
Employee Name - Sanjeet
Employee Position - Manager
Employee Salary - 98000
Size of list :0
Removed Item is : null
poll(long timeout, TimeUnit unit)
The poll(long timeout, TimeUnit unit) method of LinkedBlockingQueue returns the head of LinkedBlockingQueue by removing that element from the queue. It can be said that this method retrieves and removes element from head of this LinkedBlockingQueue. If queue is empty, then poll() method will wait till a specified time for an element to become available.
Syntax:
public E poll(long timeout, TimeUnit unit) throws
Parameters: This method takes two mandatory parameters:
- timeout– how long to wait, in units of unit.
- unit– a TimeUnit for the timeout parameter.
Return Value: This method retrieves and removes element from head of this LinkedBlockingQueue, or null if the specified waiting time elapses before an element is available.
Exception This method throws InterruptedException if method interrupted while waiting for an element to become available.
Below programs illustrates poll(long timeout, TimeUnit unit) method of LinkedBlockingQueue class:
Program 1: Removing elements from the LinkedBlockingQueue using poll() method where LinkedBlockingQueue contains list of names.
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
int capacityOfQueue = 4 ;
LinkedBlockingQueue<String> linkedQueue
= new LinkedBlockingQueue<String>(capacityOfQueue);
linkedQueue.add( "Ravi" );
linkedQueue.add( "Suraj" );
linkedQueue.add( "Harsh" );
System.out.println( "Items in Queue are " + linkedQueue);
System.out.println( "Removing item From head: "
+ linkedQueue.poll( 5 , TimeUnit.SECONDS));
System.out.println( "Now Queue Contains" + linkedQueue);
System.out.println( "Removing item From head: "
+ linkedQueue.poll( 5 , TimeUnit.SECONDS));
System.out.println( "Now Queue Contains" + linkedQueue);
System.out.println( "Removing item From head: "
+ linkedQueue.poll( 5 , TimeUnit.SECONDS));
System.out.println( "Now Queue Contains" + linkedQueue);
System.out.println( "Removing item From head: "
+ linkedQueue.poll( 5 , TimeUnit.SECONDS));
System.out.println( "Now Queue Contains" + linkedQueue);
}
}
|
Output:
Items in Queue are [Ravi, Suraj, Harsh]
Removing item From head: Ravi
Now Queue Contains[Suraj, Harsh]
Removing item From head: Suraj
Now Queue Contains[Harsh]
Removing item From head: Harsh
Now Queue Contains[]
Removing item From head: null
Now Queue Contains[]
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...