BlockingQueue take() method in Java with examples (original) (raw)
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
import
java.util.concurrent.LinkedBlockingQueue;
import
java.util.concurrent.BlockingQueue;
public
class
GFG {
`` public
static
void
main(String[] args)
`` throws
InterruptedException
`` {
`` int
capacityOfQueue =
4
;
`` BlockingQueue<String> BQ
`` =
new
LinkedBlockingQueue<String>(capacityOfQueue);
`` BQ.add(
"Ravi"
);
`` BQ.add(
"Suraj"
);
`` BQ.add(
"Harsh"
);
`` BQ.add(
"Sayan"
);
`` System.out.println(
"Items in Queue are "
+ BQ);
`` String removedItem1 = BQ.take();
`` System.out.println(
"Removed Item from head is "
`` + removedItem1);
`` System.out.println(
"Remaining Items in Queue are "
`` + BQ);
`` String removedItem2 = BQ.take();
`` System.out.println(
"Removed Item from head is "
`` + removedItem2);
`` 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
import
java.util.concurrent.LinkedBlockingQueue;
import
java.util.concurrent.BlockingQueue;
public
class
GFG {
`` public
void
takeDemo()
throws
InterruptedException
`` {
`` int
capacityOfQueue =
5
;
`` BlockingQueue<Employee> BQ
`` =
new
LinkedBlockingQueue<Employee>(capacityOfQueue);
`` Employee emp1 =
new
Employee(
"Ravi"
,
"Tester"
,
"39000"
);
`` Employee emp2 =
new
Employee(
"Sanjeet"
,
"Manager"
,
"98000"
);
`` BQ.add(emp1);
`` BQ.add(emp2);
`` while
(BQ.size() !=
0
) {
`` Employee removedEmp = BQ.take();
`` 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 = BQ.size();
`` System.out.println(
"\nSize of list :"
+ size +
"\n"
);
`` }
`` }
`` 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();
`` 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