Java的PriorityBlockingQueue優(yōu)先級(jí)阻塞隊(duì)列代碼實(shí)例
PriorityBlockingQueue阻塞隊(duì)列
PriorityBlockingQueue顧名思義是帶有優(yōu)先級(jí)的阻塞隊(duì)列,為了實(shí)現(xiàn)按優(yōu)先級(jí)彈出數(shù)據(jù),存入其中的對(duì)象必須實(shí)現(xiàn)comparable接口自定義排序方法。
取出數(shù)據(jù)時(shí)會(huì)按照compareTo方法排序后的順序取出。
首先是定義實(shí)現(xiàn)comparable接口的類:
/**
* 添加到優(yōu)先級(jí)隊(duì)列的對(duì)象需要自定義排序方法
* @author SN
*
*/
public class Product implements Comparable<Product>{
private int id;
private String name;
@Override
public int compareTo(Product product){
return this.id<product.id?-1:(this.id>product.id?1:0);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return this.id+","+this.name;
}
}然后是測(cè)試操作優(yōu)先級(jí)阻塞隊(duì)列:
import java.util.concurrent.PriorityBlockingQueue;
public class PriorityQueueExp {
//優(yōu)先級(jí)阻塞隊(duì)列添加的對(duì)象必須實(shí)現(xiàn)comparable接口才能進(jìn)行排序
static PriorityBlockingQueue<Product> pbq=new PriorityBlockingQueue<>();
public static void main(String[] args) throws InterruptedException {
Product p1=new Product();
p1.setId(1);
p1.setName("數(shù)據(jù)1");
Product p2=new Product();
p2.setId(3);
p2.setName("數(shù)據(jù)3");
Product p3=new Product();
p3.setId(2);
p3.setName("數(shù)據(jù)2");
Product p4=new Product();
p4.setId(4);
p4.setName("數(shù)據(jù)4");
pbq.add(p1);
pbq.add(p2);
pbq.add(p3);
pbq.add(p4);
System.out.println("隊(duì)列中的數(shù)據(jù):"+pbq);
System.out.println("取出隊(duì)列中的第一個(gè)數(shù)據(jù)"+pbq.take().toString());
System.out.println("隊(duì)列中的數(shù)據(jù):"+pbq);
Product p5=new Product();
p5.setId(1);
p5.setName("數(shù)據(jù)1");
pbq.add(p5);
Product p6=new Product();
p6.setId(6);
p6.setName("數(shù)據(jù)6");
pbq.add(p6);
System.out.println("隊(duì)列中的數(shù)據(jù):"+pbq);
}
}
值得注意的是,從打印結(jié)果看,數(shù)據(jù)插入隊(duì)列是按先進(jìn)先出的順序插入的,并沒(méi)有在插入隊(duì)列時(shí)就提前排好序。在第一次取出數(shù)據(jù)后再次查看隊(duì)列中的數(shù)據(jù)會(huì)發(fā)現(xiàn),隊(duì)列中的數(shù)據(jù)已經(jīng)排好序,后面在進(jìn)行任何插入、取出操作都會(huì)進(jìn)行排序,因此可以得出優(yōu)先級(jí)阻塞隊(duì)列是延遲排序的,只有在第一次取出數(shù)據(jù)后才會(huì)進(jìn)行排序。這樣應(yīng)該也是作者在設(shè)計(jì)優(yōu)先級(jí)隊(duì)列時(shí)進(jìn)行的一個(gè)性能上的優(yōu)化。類似于類的延遲加載。
到此這篇關(guān)于Java的PriorityBlockingQueue優(yōu)先級(jí)阻塞隊(duì)列代碼實(shí)例的文章就介紹到這了,更多相關(guān)PriorityBlockingQueue阻塞隊(duì)列內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- JavaEE多線程中阻塞隊(duì)列的項(xiàng)目實(shí)踐
- Java中的SynchronousQueue阻塞隊(duì)列使用代碼實(shí)例
- Java中的SynchronousQueue阻塞隊(duì)列及使用場(chǎng)景解析
- java中的BlockingQueue(阻塞隊(duì)列)解析
- Java中的BlockingQueue阻塞隊(duì)列原理以及實(shí)現(xiàn)詳解
- Java的非阻塞隊(duì)列ConcurrentLinkedQueue解讀
- java中阻塞隊(duì)列和非阻塞隊(duì)列的實(shí)現(xiàn)
- Java多線程實(shí)現(xiàn)阻塞隊(duì)列的示例代碼
相關(guān)文章
Canal搭建?idea設(shè)置及采集數(shù)據(jù)到kafka的操作方法
這篇文章主要介紹了Canal搭建idea設(shè)置及采集數(shù)據(jù)到kafka的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
基于jdk動(dòng)態(tài)代理和cglib動(dòng)態(tài)代理實(shí)現(xiàn)及區(qū)別說(shuō)明
這篇文章主要介紹了基于jdk動(dòng)態(tài)代理和cglib動(dòng)態(tài)代理實(shí)現(xiàn)及區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
基于Java實(shí)現(xiàn)多線程下載并允許斷點(diǎn)續(xù)傳
這篇文章主要介紹了基于Java實(shí)現(xiàn)多線程下載并允許斷點(diǎn)續(xù)傳,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Spring Boot監(jiān)聽(tīng)Redis Key失效事件實(shí)現(xiàn)定時(shí)任務(wù)的示例
這篇文章主要介紹了Spring Boot監(jiān)聽(tīng)Redis Key失效事件實(shí)現(xiàn)定時(shí)任務(wù)的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
springBoot項(xiàng)目中的static和templates文件夾的使用
本文主要介紹了springBoot項(xiàng)目中的static和templates文件夾的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07

