This article is the fifth in a series of articles that introduced the Tools for concurrent programming in Java: the BlockingQueue interface, ArrayBlockingQueue, DelayQueue, and LinkedBlockingQueue.

The Java PriorityBlockingQueue is an implementation class of the BlockingQueue interface that determines the order in which the elements in the queue are processed based on priority. That is, in a PriorityBlockingQueue queue, The elements added to the queue are sorted according to priority. PriorityBlockingQueue has some of the features of BlockingQueue blocking queues. If you are not familiar with BlockingQueue, refer to my previous article.

1. PriorityBlockingQueue features

  • PriorityBlockingQueue is an unbounded queue (there is no upper limit on the number of elements in the queue) that can grow automatically. Its initialization queue capacity is 11, which can also be specified with the constructor parameter initialCapacity.
  • NULL objects are not accepted to be inserted into PriorityBlockingQueue
  • The Java classes that correspond to the elements added to the PriorityBlockingQueue usually need to implement the Comparable interface or be objects (such as numbers, strings) that can be sorted by default, otherwise they will be thrownClassCastException
  • You can use Java8’s Comparator to provide a custom collation of elements in a queue, as illustrated below.
  • If there are multiple objects with equal priority, polling an element from the queue may fetch any of them.
  • PriorityBlockingQueue is thread-safe

2. PriorityBlockingQueue application instance

We write a class Employee that implements the Comparable interface, so example objects can be sorted according to the rules defined by the compareTo() function.

public class Employee implements Comparable<Employee> { private Long id; private String name; private LocalDate dob; //Getters and setters public Employee(Long id, String name, LocalDate dob) { super(); this.id = id; this.name = name; this.dob = dob; } @Override public int compareTo(Employee emp) { return this.getId().compareTo(emp.getId()); } @override public String toString() {return "Employee [id=" + id + ", name=" + name + ", dob=" + dob + "]"; }}Copy the code

Construct a PriorityBlockingQueue object, add several Employee objects to it, and use the poll method to fetch elements from the queue.

PriorityBlockingQueue<Employee> priorityBlockingQueue = new PriorityBlockingQueue<>();

priorityBlockingQueue.add(new Employee(1l, "AAA", LocalDate.now()));
priorityBlockingQueue.add(new Employee(4l, "CCC", LocalDate.now()));
priorityBlockingQueue.add(new Employee(5l, "BBB", LocalDate.now()));
priorityBlockingQueue.add(new Employee(2l, "FFF", LocalDate.now()));
priorityBlockingQueue.add(new Employee(3l, "DDD", LocalDate.now()));
priorityBlockingQueue.add(new Employee(6l, "EEE", LocalDate.now()));

while(true) {
  Employee e = priorityBlockingQueue.poll();
  System.out.println(e);

  if(e == null) break;
}
Copy the code

According to the sorting rules defined by the compareTo() method above, the order in which objects are pulled from the queue and printed is as follows:

Employee [id=1, name=AAA, dob=2021-03-25]
Employee [id=2, name=FFF, dob=2021-03-25]
Employee [id=3, name=DDD, dob=2021-03-25]
Employee [id=4, name=CCC, dob=2021-03-25]
Employee [id=5, name=BBB, dob=2021-03-25]
Employee [id=6, name=EEE, dob=2021-03-25]
Copy the code

3. Prioritizing using Java8 Comparator

We can define priority sorting using the Java 8 Comparator collator. Construct the PriorityBlockingQueue queue using the constructor PriorityBlockingQueue(int initialCapacity, Comparator Comparator).

Comparator<Employee> nameSorter = Comparator.comparing(Employee::getName); PriorityBlockingQueue<Employee> priorityBlockingQueue = new PriorityBlockingQueue<>( 11, nameSorter ); // The code for adding objects to the queue and looping out objects is omitted. See aboveCopy the code

The printing sequence is AAA, BBB, CCC, DDD, EEE, and FFF

Employee [id=1, name=AAA, dob=2021-03-25]
Employee [id=5, name=BBB, dob=2021-03-25]
Employee [id=4, name=CCC, dob=2021-03-25]
Employee [id=3, name=DDD, dob=2021-03-25]
Employee [id=6, name=EEE, dob=2021-03-25]
Employee [id=2, name=FFF, dob=2021-03-25]
Copy the code

.

Welcome to my blog, where there are many fine collections

  • This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.

Feel helpful to you, help me like, share! Your support is my inexhaustible creative power! . In addition, the author recently a period of time output as follows boutique content, looking forward to your attention.

  • Spring Boot2.0 by Hand
  • Spring Security- JWT-OAUTH2
  • RBAC Authority Management System for Actual Combat Front-end and Back-end Separation
  • “Actual SpringCloud Micro-service from Bronze to King”
  • VUE Series