Java PriorityBlockingQueue
PriorityBlockingQueue is an implementation of the BlockingQueue, Which is backed by the Array Based binary heap Data Structure. PriorityBlockingQueue can be initialized with and without the specified limit.
If you do not specify the limit then default limit will be 11 , the following are constructors of the PriorityBlockingQueue.
- PriorityBlockingQueue()
- PriorityBlockingQueue(int initialCapacity)
- PriorityBlockingQueue(int initialCapacity,Comparator<? super E> comparator)
If the added elements size reached to the initialCapacity, then PriorityBlockingQueue size will grow dynamically and will not throw any exceptions.
Elements which are adding to the PriorityBlockingQueue should implement Comparable or Comparator interface.
In case elements are implemented Comparator interface then it should be specified in the PriorityBlockingQueue Constructor.
Fetching of the elements will be done based on the implementation of Comparable or Comparator interfaces
The following are the most commonly used methods in the PriorityBlockingQueue.
Insert Operation- add(e) If this operation is not possible immediately then an exception will be thrown.
- offer(e) If this operation is not possible immediately then it will return false otherwise true.
- put(e) This operation will wait until it gets success in other words it will block the current thread until operation gets success.
- offer(e, time, unit) This operation is not possible immediately then it will until the specified time limit for success otherwise it will returns false
- remove(Object e) If the queue contains the element e then it will remove that element and return true otherwise return false. If the Object e is not compatible with Queue will lead to an exception
- poll() It will retrieve and removes the head of the queue, in case Queue is empty return null.
- take() It will retrieve and removes the head of the queue, in case Queue is empty thread will wait until the element becomes available.
- poll(time, unit) It will retrieve and removes the head of the queue, in case Queue is empty will wait until the specified time exhausted
- element() :-It will retrieve and will not remove the head of the queue, in case Queue is empty will lead to an exception
- peek():- It will retrieve and will not remove the head of the queue, in case Queue is empty returns null.
PriorityBlockingQueue Example
import java.util.Comparator; import java.util.concurrent.BlockingQueue; import java.util.concurrent.PriorityBlockingQueue; public class PriorityBlockingQueueExample { public static void main(String[] args) throws InterruptedException { BlockingQueue<Student> priorityBlockingQueue=new PriorityBlockingQueue<Student>(4,new Student()); priorityBlockingQueue.put(new Student(4)); priorityBlockingQueue.put(new Student(2)); priorityBlockingQueue.put(new Student(3)); priorityBlockingQueue.put(new Student(1)); System.out.println(priorityBlockingQueue.take().id); System.out.println(priorityBlockingQueue.take().id); System.out.println(priorityBlockingQueue.take().id); System.out.println(priorityBlockingQueue.take().id); } } class Student implements Comparator<Student> { Integer id; public Student(Integer id) { this.id=id; } public Student() { } @Override public int compare(Student s1,Student s2) { return s1.id.compareTo(s2.id); } }Output
Student Id :1 Student Id :2 Student Id :3 Student Id :4