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.
Java
// Java Program Demonstrate poll()
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 4;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<String> linkedQueue
= new LinkedBlockingQueue<String>(capacityOfQueue);
// Add element to LinkedBlockingQueue
linkedQueue.add("Ravi");
linkedQueue.add("Suraj");
linkedQueue.add("Harsh");
linkedQueue.add("Sayan");
// print elements of queue
System.out.println("Items in Queue are " + linkedQueue);
// we want to remove two elements from queue from head
// Applying poll() method on queue to remove element
String removedItem1 = linkedQueue.poll();
// print removedItem and queue
System.out.println("Removed Item is " + removedItem1);
// print elements of queue after removing first item
System.out.println("Remaining Items in Queue are "
+ linkedQueue);
// Applying poll() method on queue to remove another element
String removedItem2 = linkedQueue.poll();
// print removedItem and queue
System.out.println("Removed Item is " + removedItem2);
// print elements of queue after removing first item
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.
Java
// Java Program Demonstrate poll()
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public void pollingMethod()
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 5;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<Employee> linkedQueue
= new LinkedBlockingQueue<Employee>(capacityOfQueue);
// Add element to LinkedBlockingQueue
Employee emp1 = new Employee("Ravi", "Tester", "39000");
Employee emp2 = new Employee("Sanjeet", "Manager", "98000");
// Add Employee Objects to linkedQueue
linkedQueue.add(emp1);
linkedQueue.add(emp2);
// remove elements from the queue
// and follow this process again and again
// until the queue becomes empty
while (linkedQueue.size() != 0) {
// Remove Employee item from LinkedBlockingQueue
Employee removedEmp = linkedQueue.poll();
// print removedItem
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);
// find size of linkedQueue
int size = linkedQueue.size();
// print remaining capacity value
System.out.println("\nSize of list :" + size + "\n");
}
// Now size of Queue is Zero
// Now try to Poll more items
// Remove Employee item from LinkedBlockingQueue
Employee removedEmp = linkedQueue.poll();
// print removedItem
// size is zero so removedItem is null
System.out.println("Removed Item is : " + removedEmp);
}
// create an Employee Object with name,
// position and salary as an attribute
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 + "]";
}
}
// Main Method
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.
Java
// Java program to demonstrate
// poll(long timeout, TimeUnit unit)
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 4;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<String> linkedQueue
= new LinkedBlockingQueue<String>(capacityOfQueue);
// Add element to LinkedBlockingQueue
linkedQueue.add("Ravi");
linkedQueue.add("Suraj");
linkedQueue.add("Harsh");
// print elements of queue
System.out.println("Items in Queue are " + linkedQueue);
// Try to poll elements from linkedQueue
// using poll(long timeout, TimeUnit unit) method
System.out.println("Removing item From head: "
+ linkedQueue.poll(5, TimeUnit.SECONDS));
// print queue details
System.out.println("Now Queue Contains" + linkedQueue);
// using poll(long timeout, TimeUnit unit) method
System.out.println("Removing item From head: "
+ linkedQueue.poll(5, TimeUnit.SECONDS));
// print queue details
System.out.println("Now Queue Contains" + linkedQueue);
// using poll(long timeout, TimeUnit unit) method
System.out.println("Removing item From head: "
+ linkedQueue.poll(5, TimeUnit.SECONDS));
// print queue details
System.out.println("Now Queue Contains" + linkedQueue);
// using poll(long timeout, TimeUnit unit) method
System.out.println("Removing item From head: "
+ linkedQueue.poll(5, TimeUnit.SECONDS));
// print queue details
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:
Similar Reads
LinkedBlockingQueue Class in Java
The LinkedBlockingQueue in Java is part of the java.util.concurrent package and implements the BlockingQueue interface. It provides a thread-safe, bounded, or unbounded queue used for managing tasks in a producer-consumer scenario. This queue can be used in multithreaded environments where one threa
8 min read
LinkedBlockingQueue clear() method in Java
The clear() method of LinkedBlockingQueue removes all of the elements from this queue. After applying this method the queue will become empty. Syntax: public void clear() Below programs illustrates clear() method of LinkedBlockingQueue class: Program 1: Java // Java Program Demonstrate clear() // me
2 min read
LinkedBlockingQueue iterator() method in Java
The iterator() method of LinkedBlockingQueue returns an iterator of the same elements, as this LinkedBlockingQueue, in a proper sequence. The elements returned from this method contains all the elements in order from first(head) to last(tail) of LinkedBlockingQueue. The returned iterator is weakly c
3 min read
LinkedBlockingQueue drainTo() method in Java
The drainTo(Collection col) method of LinkedBlockingQueue removes all available elements from this LinkedBlocking Queue and adds them to the given collection passed as a parameter. drainTo(Collection<? super E> col) The drainTo(Collection<? super E> col) method of LinkedBlockingQueue rem
7 min read
LinkedBlockingQueue | offer() Method in JAVA
There is two types of offer() method for LinkedBlockingQueue class : offer(E e, long timeout, TimeUnit unit) The offer(E e, long timeout, TimeUnit unit) method of LinkedBlockingQueue inserts the element passed as parameter to method at the tail of this LinkedBlockingQueue if queue is not full. It wi
6 min read
LinkedBlockingQueue peek() method in Java
The peek() method of LinkedBlockingQueue returns the head of the LinkedBlockingQueue. It retrieves the value of the head of LinkedBlockingQueue but does not remove it. If the LinkedBlockingQueue is empty then this method returns null. Syntax: public E peek() Return Value: This method returns the hea
3 min read
LinkedBlockingQueue poll() method in Java
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 em
6 min read
LinkedBlockingQueue put() method in Java with Examples
The put(E e) method of LinkedBlockingQueue inserts element passed as parameter to method at the tail of this LinkedBlockingQueue, if queue is not full. If the queue is full, then this method will wait for space to become available and after space is available, it inserts the element to LinkedBlockin
3 min read
LinkedBlockingQueue remainingCapacity() method in Java
The remainingCapacity() method of LinkedBlockingQueue returns the number of more elements that can be added to LinkedBlockingQueue without blocking. The Capacity returned arises three cases: If remaining Capacity is Zero, then no more elements can be added to the LinkedBlockingQueue.If remaining Cap
3 min read
LinkedBlockingQueue remove() method in Java
The remove(Object obj) method of LinkedBlockingQueue removes only one instance of the given Object, passed as parameter, from this LinkedBlockingQueue if it is present. It removes an element e such that obj.equals(e) and if this queue contains one or more instance of element e. This method returns t
4 min read