BlockingQueue take() method in Java with examples
Last Updated :
23 Aug, 2021
The take() method of BlockingQueue interface is used to retrieve and remove the head of this queue. If the queue is empty then it will wait until an element becomes available. This method is more efficient if working on threads and using BlockingQueue in that process. So the thread that initially calls take() goes to sleep if there is no element available, letting other threads do whatever they need to do.
Syntax:
public E take() throws InterruptedException
Return Value: This method returns value at the head of this BlockingQueue. If the queue is empty then it will wait until an element becomes available.
Exception: This method throws following exceptions:
- InterruptedException- When the interruption occurs at time of waiting for an element to become available if queue is empty.
Note: The take() method of BlockingQueue has been inherited from the Queue class in Java.
Below programs illustrates take() method of BlockingQueue interface:
Program 1:
Java
// Java Program Demonstrate take()
// method of BlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// define capacity of BlockingQueue
int capacityOfQueue = 4;
// create object of BlockingQueue
BlockingQueue<String> BQ
= new LinkedBlockingQueue<String>(capacityOfQueue);
// Add element to BlockingQueue
BQ.add("Ravi");
BQ.add("Suraj");
BQ.add("Harsh");
BQ.add("Sayan");
// print elements of queue
System.out.println("Items in Queue are " + BQ);
// remove two elements from queue from head
// Applying take() method on queue to remove element
String removedItem1 = BQ.take();
// print removedItem and queue
System.out.println("Removed Item from head is "
+ removedItem1);
// print elements of queue after removing first item
System.out.println("Remaining Items in Queue are "
+ BQ);
// Applying take() method on queue to remove another element
String removedItem2 = BQ.take();
// print removedItem and queue
System.out.println("Removed Item from head is "
+ removedItem2);
// print elements of queue after removing first item
System.out.println("Remaining Items in Queue are "
+ BQ);
}
}
Output: Items in Queue are [Ravi, Suraj, Harsh, Sayan]
Removed Item from head is Ravi
Remaining Items in Queue are [Suraj, Harsh, Sayan]
Removed Item from head is Suraj
Remaining Items in Queue are [Harsh, Sayan]
Program 2:
Java
// Java Program Demonstrate take()
// method of BlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
public void takeDemo() throws InterruptedException
{
// define capacity of BlockingQueue
int capacityOfQueue = 5;
// create object of BlockingQueue
BlockingQueue<Employee> BQ
= new LinkedBlockingQueue<Employee>(capacityOfQueue);
// Add element to BlockingQueue
Employee emp1 = new Employee("Ravi", "Tester", "39000");
Employee emp2 = new Employee("Sanjeet", "Manager", "98000");
// Add Employee Objects to BQ
BQ.add(emp1);
BQ.add(emp2);
// remove elements from the queue
// and follow this process again and again
// until the queue becomes empty
while (BQ.size() != 0) {
// Remove Employee item from BlockingQueue
// using take()
Employee removedEmp = BQ.take();
// 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 BQ
int size = BQ.size();
// print remaining capacity value
System.out.println("\nSize of list :" + size + "\n");
}
}
// 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();
try {
gfg.takeDemo();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
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
Similar Reads
BlockingDeque take() method in Java with Examples The take() method of BlockingDeque returns and removes the head of the Deque container from it. The method throws an InterruptedException if it is interrupted while waiting. Syntax: public E take() Returns: This method returns the head of the Deque container. Exception: The function throws a Interru
2 min read
BlockingQueue put() method in Java with examples The put(E e) method of BlockingQueue interface inserts element passed as parameter to method at the tail of this BlockingQueue, 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 BlockingQueue.
3 min read
BlockingQueue offer() method in Java with examples There are two types of offer() method for BlockingQueue interface:Note: The offer() method of BlockingQueue has been inherited from the Queue class in Java. offer(E e, long timeout, TimeUnit unit) The offer(E e, long timeout, TimeUnit unit) method of BlockingQueue inserts the element passed as param
6 min read
BlockingDeque takeLast() method in Java with Examples The takeLast() method of BlockingDeque returns and removes the tail of the Deque container from it. The method throws an InterruptedException if it is interrupted while waiting. Syntax:  public E takeLast() Returns: This method returns the tail(last element) of the Deque container. Exception: The
2 min read
BlockingQueue remove() method in Java with examples The remove(Object obj) method of BlockingQueue removes only one instance of the given Object, passed as parameter, from this BlockingQueue 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 true if this
4 min read
BlockingDeque takeFirst() method in Java with Examples The takeFirst() method of BlockingDeque returns and removes the first element of the Deque container from it, waiting if necessary until an element becomes available.. The method throws an InterruptedException if it is interrupted while waiting. Syntax: public E takeFirst() Returns: This method retu
2 min read