Python 官方文档:入门教程 => 点击学习
目录前言1. PriorityBlockingQueue 特性2. PriorityBlockingQueue 应用实例3. 使用 Java8 Comparator 做优先级排序的实
在之前的文章中已经为大家介绍了java并发编程的工具:BlockingQueue接口、ArrayBlockingQueue、DelayQueue、LinkedBlockingQueue,本文为系列文章第五篇。
Java PriorityBlockingQueue队列是BlockingQueue接口的实现类,它根据priority优先级确定队列内元素对象的处理顺序,也就是说在一个PriorityBlockingQueue队列中,被添加到队列中的元素,根据priority进行排序。PriorityBlockingQueue具有BlockingQueue阻塞队列的一些特性,如果您不熟悉BlockingQueue可以参看我之前的文章。
我们写一个类Employee
,该类实现了Comparable接口,所以其实例对象可以根据compareTo()函数定义的规则进行排序。
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 + "]";
}
}
构造一个PriorityBlockingQueue对象,并向其内部加入若干Employee对象,并使用poll方法从队列内取出元素。
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;
}
根据上文中compareTo()方法定义的排序规则,按照id为优先级,所以从队列中拿出对象并打印的顺序如下:
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]
我们可以使用java 8 Comparator排序器,来定义优先级排序规则。使用构造方法PriorityBlockingQueue(int initialCapacity, Comparator comparator) 构造PriorityBlockingQueue队列。
//以员工名称的字符串自然正序进行排序
Comparator<Employee> nameSorter = Comparator.comparing(Employee::getName);
PriorityBlockingQueue<Employee> priorityBlockingQueue = new PriorityBlockingQueue<>( 11, nameSorter );
//此处省略向队列中添加对象,及循环取出对象打印的代码,参考上文
按照员工姓名进行优先级排序,所以打印顺序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]
以上就是java并发编程工具类PriorityBlockingQueue优先级队列的详细内容,更多关于java并发PriorityBlockingQueue队列的资料请关注编程网其它相关文章!
--结束END--
本文标题: java并发编程工具类PriorityBlockingQueue优先级队列
本文链接: https://lsjlt.com/news/142649.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0