Java concurrent programming tool class JUC fifth: PriorityBlockingQueue priority queue

In the previous article, I have introduced the tools of java concurrent programming: BlockingQueue interface, ArrayBlockingQueue, DelayQueue, LinkedBlockingQueue. This article is the fifth article in the series.

The Java PriorityBlockingQueue queue is an implementation class of the BlockingQueue interface. It determines the processing order of the element objects in the queue according to the priority priority, that is, in a PriorityBlockingQueue queue, the elements added to the queue are sorted according to the priority. PriorityBlockingQueue has some features of BlockingQueue. If you are not familiar with BlockingQueue, you can refer to my previous article.

1. PriorityBlockingQueue 特性

  • PriorityBlockingQueue is an unbounded queue (there is no upper limit on the number of elements in the queue), and the queue capacity can automatically grow. The initial queue capacity is 11, and the initial capacity can also be specified through the constructor parameter initialCapacity.
  • Not accept NULL objects inserted into PriorityBlockingQueue
  • The Java class corresponding to the element added to the PriorityBlockingQueue queue usually needs to implement the Comparable interface or an object that can be sorted by default (such as numbers, strings), otherwise it will be thrownClassCastException
  • You can use java8's Comparator to provide custom sorting rules for the elements in the queue, which will be illustrated later.
  • If there are multiple objects with equal priority, any one of the elements may be obtained when the element is obtained from the poll in the queue.
  • PriorityBlockingQueue is thread-safe

2. PriorityBlockingQueue application example

We write a class Employeethat implements the Comparable interface, so its instance 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());  //根据id排序
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + ", dob=" + dob + "]";
    }
}

Construct a PriorityBlockingQueue object, add several Employee objects to it, and use the poll method to remove 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;
}

According to the sorting rules defined by the compareTo() method above, according to id as the priority, the order of taking out objects from the queue and printing them 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]

3. Example of using Java8 Comparator to prioritize

We can use the java 8 Comparator sorter to define priority sorting rules. Use the constructor PriorityBlockingQueue(int initialCapacity, Comparator comparator) to construct the PriorityBlockingQueue queue.

//以员工名称的字符串自然正序进行排序
Comparator<Employee> nameSorter = Comparator.comparing(Employee::getName);

PriorityBlockingQueue<Employee> priorityBlockingQueue = new PriorityBlockingQueue<>( 11, nameSorter );

//此处省略向队列中添加对象,及循环取出对象打印的代码,参考上文

Prioritize according to employee name, so the printing order is AAA, BBB, CCC, DDD, EEE, 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]

.

Welcome to follow my blog, there are many boutique collections

  • This article is reproduced indicate the source (en must not turn only the text): letters Gebo off .

If you think it is helpful to you, please like and share it for me! Your support is my inexhaustible creative motivation! . In addition, the author has output the following high-quality content recently, and I look forward to your attention.

Guess you like

Origin blog.csdn.net/hanxiaotongtong/article/details/115223260
Recommended