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.
Syntax:
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.
// Java Program Demonstrate take() // method of PriorityBlockingQueue import java.util.concurrent.PriorityBlockingQueue; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // create object of PriorityBlockingQueue PriorityBlockingQueue<Integer> PrioQueue = new PriorityBlockingQueue<Integer>(); // Add numbers to PriorityBlockingQueue PrioQueue.put(7855642); PrioQueue.put(35658786); PrioQueue.put(5278367); PrioQueue.put(74381793); // before removing print queue System.out.println("Queue: " + PrioQueue); // Apply take() method int head = PrioQueue.take(); // Print head of queue using take() method System.out.println("Head of PriorityBlockingQueue" + " using take(): " + head); System.out.print("After removing head, Queue: " + PrioQueue); } } |
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
// Java Program Demonstrate take() // method of PriorityBlockingQueue import java.util.concurrent.PriorityBlockingQueue; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // create object of PriorityBlockingQueue // which contains Strings PriorityBlockingQueue<String> names = new PriorityBlockingQueue<String>(); // Add string names.add("Geeks"); names.add("forGeeks"); names.add("A computer portal"); // print list of names System.out.println(names); // Apply take() method String head = names.take(); // Print head of queue using take() method System.out.println("Head of Queue: " + head); System.out.print("After removing head, Queue: " + names); } } |
[A computer portal, forGeeks, Geeks] Head of Queue: A computer portal After removing head, Queue: [Geeks, forGeeks]
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#take–
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- PriorityBlockingQueue put() method in Java
- PriorityBlockingQueue size() method in Java
- PriorityBlockingQueue remainingCapacity() method in Java
- PriorityBlockingQueue remove() method in Java
- PriorityBlockingQueue clear() method in Java
- PriorityBlockingQueue add() Method in Java
- PriorityBlockingQueue contains() method in Java
- PriorityBlockingQueue iterator() method in Java
- PriorityBlockingQueue peek() method in Java
- PriorityBlockingQueue toString() method in Java
- PriorityBlockingQueue comparator() method in Java
- PriorityBlockingQueue drainTo() method in Java
- PriorityBlockingQueue spliterator() method in Java
- PriorityBlockingQueue toArray() method in Java
- PriorityBlockingQueue poll() method in Java
- PriorityBlockingQueue offer() method in Java
- PriorityBlockingQueue Class in Java
- ArrayBlockingQueue take() method in Java
- LinkedBlockingQueue take() Method in Java with Examples
- LinkedTransferQueue take() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

