PriorityBlockingQueue详解

news/2024/7/5 2:04:40

1.PriorityBlockingQueue

public class PriorityBlockingQueue<E>
extends AbstractQueue<E>
implements BlockingQueue<E>, Serializable

  所有添加进入PriorityBlockingQueue的元素都必须实现Comparable接口。当插入元素时,PriorityBlockingQueue会使用CompareTo()方法来决定元素插入的位置,元素越大越靠后。PriorityBlockingQueue是一个阻塞式的数据结构。当它的方法被调用且不能立即执行时,调用这个方法的线程将被阻塞直到方法执行完成。

public class Event implements Comparable<Event> {private int thread;private int priority;public Event(int thread, int priority) {super();this.thread = thread;this.priority = priority;}public int getThread() {return thread;}public int getPriority() {return priority;}@Overridepublic int compareTo(Event o) {if(this.priority>o.getPriority()){return -1;}else if(this.priority<o.getPriority()){return 1;}else{return 0;}}
}
public class Task implements Runnable {private int id;private PriorityBlockingQueue<Event> queue;public Task(int id, PriorityBlockingQueue<Event> queue) {super();this.id = id;this.queue = queue;}@Overridepublic void run() {for (int i = 0; i < 100; i++) {Event event = new Event(id, i);queue.add(event);}}
}
public class PriorityBlockingQueueMain {public static void main(String[] args) {PriorityBlockingQueue<Event> queue = new PriorityBlockingQueue<Event>();Thread threads[] = new Thread[5];for (int i = 0; i < threads.length; i++) {Task task = new Task(i, queue);threads[i] = new Thread(task);}for (int i = 0; i < threads.length; i++) {threads[i].start();}for (int i = 0; i < threads.length; i++) {try {threads[i].join();} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("Main:Queue Size:" + queue.size());for (int i = 0; i < threads.length * 100; i++) {Event event = queue.poll();System.out.println("Thread "+ event.getThread()+" :Priority "+event.getPriority());}System.out.println("Main:Queue Size:" + queue.size());System.out.println("Main: End");}
}

转载于:https://www.cnblogs.com/wxgblogs/p/5464710.html


http://lihuaxi.xjx100.cn/news/244612.html

相关文章

bats-Bash自动化测试工具

本文分析了bats--Bash自动化测试工具的安装、语法、常用指令及常用函数等内容。上篇文章回顾&#xff1a;学习RAID 01/10/10E的区别bats 是一个符合 TAP 标准 的 Bash 版测试框架&#xff0c;它使用了一种极为简便的方法来验证命令行程序是否正常运行。bats 要求 Bash 的最低版…

SpringBoot如何处理java内存溢出

在上线的项目中&#xff0c;本地测试没有问题&#xff0c;部署上去就会出现java 内存溢出 java.lang.OutOfMemoryError: Java heap space 解决方案&#xff1a; -Xms512m -Xmx512m 问题得到解决

Java面试题汇总及答案2021最新(ioNio)

Java面试题汇总及答案2021最新&#xff08;io&Nio&#xff09; 最近给大家整理了一批Java关于io和nio的面试题一共15题&#xff0c;是20201最新时间整理的&#xff0c;并且都含答案打包下载。 适宜阅读人群 需要面试的初/中/高级 java 程序员 想要查漏补缺的人 想要不断…

maven基础概念学习1

2019独角兽企业重金招聘Python工程师标准>>> 1.maven是什么&#xff1f; 百度百科&#xff1a; Maven是基于项目对象模型(POM)&#xff0c;可以通过一小段描述信息来管理项目的构建&#xff0c;报告和文档的软件项目管理工具。 2.maven的作用&#xff1f; 构建、文档…

webpack 最简打包结果分析

原文链接&#xff1a;https://ssshooter.com/2019-02... 现在的 webpack 不再是入门噩梦,过去 webpack 最让人心塞的莫过于配置文件&#xff0c;而 webpack4 诞生随之而来的是无配置 webpack。 使用 webpack4&#xff0c;至少只需要安装 webpack 和 webpack cli。所以大家完全可…

第一天

jvm : java vitrual machine,java虚拟机&#xff08;类似sandbox&#xff09;jre : java runtime environment java运行时环境jvm 核心类库.jdk : java development kit ,java开发包jre 工具软件。转载于:https://blog.51cto.com/11306424/1770775

Java面试题汇总及答案2021最新(Java基础含答案下载)

Java面试题汇总及答案2021最新&#xff08;Java基础含答案下载&#xff09; 为大家整理了2021最新的Java面试题及答案下载&#xff0c;这套Java面试题总汇已经汇总了Java基础面试到高级Java面试题&#xff0c;几乎涵盖了作为一个Java程序员在面试中需要掌握或者可能被问到的绝…

com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1169 1024)

需求&#xff1a; 在更改头像的时候报这个错误 解决方案&#xff1a; show VARIABLES like %max_allowed_packet%; 在mysql安装文件目录找到my.conf 重启mysql服务 查看是否修改成功&#xff1a;