PriorityBlockingQueue take() method in Java
The take() method of PriorityBlockingQueue returns head of the queue after removing it. If queue is empty, then this method will wait until an element becomes available.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
public E take() throws InterruptedException
Returns: This method returns value at the head of this PriorityBlockingQueue.
Exception: This method throws InterruptedException, if interrupted while waiting for an element to become available.
Below programs illustrate take() method of PriorityBlockingQueue:
Example 1: To demonstrate take() method on PriorityBlockingQueue which contains a list of numbers.
Queue: [5278367, 35658786, 7855642, 74381793] Head of PriorityBlockingQueue using take(): 5278367 After removing head, Queue: [7855642, 35658786, 74381793]
Example 2: To demonstrate take() method on PriorityBlockingQueue which contains String
[A computer portal, forGeeks, Geeks] Head of Queue: A computer portal After removing head, Queue: [Geeks, forGeeks]