写点什么

并发队列:PriorityBlockingQueue 和 DelayQueue 案例使用

用户头像
叫练
关注
发布于: 2021 年 02 月 05 日
并发队列:PriorityBlockingQueue和DelayQueue案例使用

PriorityBlockingQueue 使用



import java.util.concurrent.PriorityBlockingQueue;
/** * @author :jiaolian * @date :Created in 2021-02-03 15:44 * @description:priorityBlockingQueue优先级队列测试 * @modified By: * 公众号:叫练 */public class PriorityBlockingQueueTest {
public static void main(String[] args) { m2(); }
public static void m2() { PriorityBlockingQueue<Student> priorityBlockingQueue = new PriorityBlockingQueue<>(); priorityBlockingQueue.add(new Student("叫练1",22)); priorityBlockingQueue.add(new Student("叫练2",21)); priorityBlockingQueue.add(new Student("叫练3",23)); while (!priorityBlockingQueue.isEmpty()) { Student student = null; try { student = priorityBlockingQueue.take(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(student); } }
private static class Student implements Comparable<Student> { private String name; private int age;
public Student(String name, int age) { this.name = name; this.age = age; }
public String getName() { return name; }
public int getAge() { return age; }
@Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; }
@Override public int compareTo(Student o) { //从大到小排队 return this.age - o.getAge(); } }}
复制代码

如上代码:PriorityBlockingQueue 优先级队列PriorityBlockingQueue 队列的元素需要实现 Comparable 接口,完成队列的排序,上述代码中定义了 Student 类实现 Comparable 接口,Student 的 compareTo 接口按照 age 属性从小到大排队,输出结果如下图所示。PriorityBlockingQueue 底层是二叉平衡树的数组结构实现出队和入队。


image.png


DelayQueue 使用



import java.util.concurrent.DelayQueue;import java.util.concurrent.Delayed;import java.util.concurrent.TimeUnit;
/** * @author :jiaolian * @date :Created in 2021-02-03 16:28 * @description:延迟队列测试 * @modified By: * 公众号:叫练 */public class DelayQueueTest {
public static void main(String[] args) throws InterruptedException { DelayQueue<Student> delayQueue = new DelayQueue<Student>(); delayQueue.add(new Student("叫练1",5)); delayQueue.add(new Student("叫练2",3)); delayQueue.add(new Student("叫练3",6)); while (!delayQueue.isEmpty()) { System.out.println(delayQueue.take()); } }

private static class Student implements Delayed { private String name; //触发时间/秒 private long time;
public String getName() { return name; }

public long getTime() { return time; }
public Student(String name, long time) { this.name = name; this.time = time*1000+System.currentTimeMillis(); }
@Override public long getDelay(TimeUnit unit) { //延迟时间小于0就出队列 long aa = time - System.currentTimeMillis(); return aa; }
@Override public int compareTo(Delayed o) { //时间排序,从小到大排列 Student student = (Student) o; return (int)(this.time - student.getTime()); }
@Override public String toString() { return "Student{" + "name='" + name + '\'' + ", time=" + time + '}'; } }}
复制代码

如上代码:DelayQueue 是延时队列,只有到指定时间的队列才可以出队列,底层使用优先级队列,上面代码定义 Student 类需要实现 Delayed 接口同时需要实现 getDelay 方法和 compareTo 方法,getDelay 方法用于计算出队列时间,一旦小于 0 就会出队列;compareTo 方法用于按触发时间从小到大排序。执行程序后,学生“叫练 2”3 秒后出队列;学生“叫练 1”5 秒后出队列;学生“叫练 3”6 秒后出队列。执行结果如下图所示。


image.png


总结


今天我们的内容比较简单。喜欢的请点赞和评论哦!点关注,不迷路,我是叫练【公众号】,边叫边练。期待我们下次再见!


发布于: 2021 年 02 月 05 日阅读数: 10
用户头像

叫练

关注

我是叫练,边叫边练 2020.06.11 加入

Java高级工程师,熟悉多线程,JVM

评论

发布
暂无评论
并发队列:PriorityBlockingQueue和DelayQueue案例使用