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

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

叫练发布于 今天 03:05

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底层是二叉平衡树的数组结构实现出队和入队。

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

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秒后出队列。执行结果如下图所示。

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

总结


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

java队列优先级并发编程

阅读 43发布于 今天 03:05

本作品系原创,采用《署名-非商业性使用-禁止演绎 4.0 国际》许可协议

avatar

叫练

持续技术输出

6 声望

0 粉丝

0 条评论

得票时间

avatar

叫练

持续技术输出

6 声望

0 粉丝

宣传栏

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底层是二叉平衡树的数组结构实现出队和入队。

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

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秒后出队列。执行结果如下图所示。

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

总结


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

以上是 【Java】并发队列:PriorityBlockingQueue和DelayQueue案例使用 的全部内容, 来源链接: www.h5w3.com/113355.html

回到顶部