java并發(fā)編程工具類PriorityBlockingQueue優(yōu)先級隊(duì)列
前言
在之前的文章中已經(jīng)為大家介紹了java并發(fā)編程的工具:BlockingQueue接口、ArrayBlockingQueue、DelayQueue、LinkedBlockingQueue,本文為系列文章第五篇。
Java PriorityBlockingQueue隊(duì)列是BlockingQueue接口的實(shí)現(xiàn)類,它根據(jù)priority優(yōu)先級確定隊(duì)列內(nèi)元素對象的處理順序,也就是說在一個PriorityBlockingQueue隊(duì)列中,被添加到隊(duì)列中的元素,根據(jù)priority進(jìn)行排序。PriorityBlockingQueue具有BlockingQueue阻塞隊(duì)列的一些特性,如果您不熟悉BlockingQueue可以參看我之前的文章。
1. PriorityBlockingQueue 特性
- PriorityBlockingQueue 是一個無界隊(duì)列(隊(duì)列內(nèi)元素個數(shù)沒有上限),隊(duì)列容量可以自動增長。其初始化隊(duì)列容量為11,也可以通過構(gòu)造函數(shù)參數(shù)initialCapacity指定其初始化容量。
- 不接受 NULL對象插入到PriorityBlockingQueue
- 添加到PriorityBlockingQueue隊(duì)列中的元素對應(yīng)的Java類,通常需要實(shí)現(xiàn)Comparable接口或者是可以默認(rèn)排序的對象(如數(shù)字、字符串),否則會拋出ClassCastException
- 可以使用java8 的Comparator提供自定義隊(duì)列內(nèi)元素的排序規(guī)則,后文會舉例說明。
- 如果存在多個對象擁有相等的優(yōu)先級,從隊(duì)列中poll獲取元素的時候可能獲取到其中任何一個元素。
- PriorityBlockingQueue 是線程安全的
2. PriorityBlockingQueue 應(yīng)用實(shí)例
我們寫一個類Employee
,該類實(shí)現(xiàn)了Comparable接口,所以其實(shí)例對象可以根據(jù)compareTo()函數(shù)定義的規(guī)則進(jìn)行排序。
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()); //根據(jù)id排序 } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", dob=" + dob + "]"; } }
構(gòu)造一個PriorityBlockingQueue對象,并向其內(nèi)部加入若干Employee對象,并使用poll方法從隊(duì)列內(nèi)取出元素。
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; }
根據(jù)上文中compareTo()方法定義的排序規(guī)則,按照id為優(yōu)先級,所以從隊(duì)列中拿出對象并打印的順序如下:
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. 使用 Java8 Comparator 做優(yōu)先級排序的實(shí)例
我們可以使用java 8 Comparator排序器,來定義優(yōu)先級排序規(guī)則。使用構(gòu)造方法PriorityBlockingQueue(int initialCapacity, Comparator comparator) 構(gòu)造PriorityBlockingQueue隊(duì)列。
//以員工名稱的字符串自然正序進(jìn)行排序 Comparator<Employee> nameSorter = Comparator.comparing(Employee::getName); PriorityBlockingQueue<Employee> priorityBlockingQueue = new PriorityBlockingQueue<>( 11, nameSorter ); //此處省略向隊(duì)列中添加對象,及循環(huán)取出對象打印的代碼,參考上文
按照員工姓名進(jìn)行優(yōu)先級排序,所以打印順序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并發(fā)編程工具類PriorityBlockingQueue優(yōu)先級隊(duì)列的詳細(xì)內(nèi)容,更多關(guān)于java并發(fā)PriorityBlockingQueue隊(duì)列的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringMvc使用GoogleKaptcha生成驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了SpringMvc項(xiàng)目中使用GoogleKaptcha 生成驗(yàn)證碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09通過Java實(shí)現(xiàn)添加或刪除PDF中的附件
當(dāng)我們在制作PDF文件或者PPT演示文稿的時候,為了讓自己的文件更全面詳細(xì),就會在文件中添加附件。本文為大家整理了Java實(shí)現(xiàn)添加或刪除PDF中的附件的方法,需要的可以參考下2023-01-01Spring?MVC異步上傳、跨服務(wù)器上傳和文件下載功能實(shí)現(xiàn)
這篇文章主要介紹了Spring?MVC異步上傳、跨服務(wù)器上傳和文件下載功能實(shí)現(xiàn),本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07Dwr3.0純注解(純Java Code配置)配置與應(yīng)用淺析一之零配置文件化
Dwr對我來說最重要的功能點(diǎn)就是反向Ajax調(diào)用,通俗來將就是后端可以直接調(diào)用前端的JS方法(只要在所能訪問的范圍內(nèi)),這也就是Dwr的真正來由,當(dāng)然它也有最基本的前端直接調(diào)用后端的特性,省去了我們經(jīng)常的一般Ajax調(diào)用2016-04-04Java多線程事務(wù)回滾@Transactional失效處理方案
這篇文章主要介紹了Java多線程事務(wù)回滾@Transactional失效處理方案,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-08-08springboot整合RabbitMQ 中的 TTL實(shí)例代碼
TTL 是 RabbitMQ 中一個消息或者隊(duì)列的屬性,表明一條消息或者該隊(duì)列中的所有消息的最大存活時間,單位是毫秒,這篇文章主要介紹了springboot整合RabbitMQ 中的 TTL,需要的朋友可以參考下2022-09-09Java 同步鎖(synchronized)詳解及實(shí)例
這篇文章主要介紹了Java 同步鎖(synchronized)詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03