# 常用阻塞队列 BlockingQueue 有哪些？

2019/04/10 10:10

## 常用的阻塞队列

<br> **1） ArrayBlockingQueue**

3） SynchronousQueue

4）PriorityBlockingQueue

PriorityBlockingQueue 是基于二叉树最小堆实现的，每当取元素的时候，就会把优先级最高的元素取出来。我们测试一下：

``````public class Person {
private int id;
private String name;

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;
}

@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}

public Person(int id, String name) {
this.id = id;
this.name = name;
}

public Person() {
}
}

public class QueueTest {
public static void main(String[] args) throws InterruptedException {

PriorityBlockingQueue<Person> priorityBlockingQueue = new PriorityBlockingQueue<>(1, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.getId() - o2.getId();
}
});

Person p2 = new Person(7, "李四");
Person p1 = new Person(9, "张三");
Person p3 = new Person(6, "王五");
Person p4 = new Person(2, "赵六");

//由于二叉树最小堆实现，用这种方式直接打印元素，不能保证有序
System.out.println(priorityBlockingQueue);
System.out.println(priorityBlockingQueue.take());
System.out.println(priorityBlockingQueue);
System.out.println(priorityBlockingQueue.take());
System.out.println(priorityBlockingQueue);

}
}
``````

``````[Person{id=2, name='赵六'}, Person{id=6, name='王五'}, Person{id=7, name='李四'}, Person{id=9, name='张三'}]
Person{id=2, name='赵六'}
[Person{id=6, name='王五'}, Person{id=9, name='张三'}, Person{id=7, name='李四'}]
Person{id=6, name='王五'}
[Person{id=7, name='李四'}, Person{id=9, name='张三'}]
``````

5）DelayQueue

``````public class DelayData implements Delayed {

private int id;
private String name;
//数据到期时间
private long endTime;
private TimeUnit timeUnit = TimeUnit.MILLISECONDS;

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 long getEndTime() {
return endTime;
}

public void setEndTime(long endTime) {
this.endTime = endTime;
}

public DelayData(int id, String name, long endTime) {
this.id = id;
this.name = name;
//需要把传入的时间endTime 加上当前系统时间，作为数据的到期时间
this.endTime = endTime + System.currentTimeMillis();
}

public DelayData() {
}

@Override
public long getDelay(TimeUnit unit) {
return this.endTime - System.currentTimeMillis();
}

@Override
public int compareTo(Delayed o) {
return o.getDelay(this.timeUnit) - this.getDelay(this.timeUnit) < 0 ? 1: -1;
}

}

``````

``````public class ProcessData {
public static void main(String[] args) throws InterruptedException {
DelayQueue<DelayData> delayQueue = new DelayQueue<>();

DelayData a = new DelayData(5, "A", 5000);
DelayData b = new DelayData(8, "B", 8000);
DelayData c = new DelayData(2, "C", 2000);

System.out.println("开始计时时间:" + System.currentTimeMillis());
for (int i = 0; i < 3; i++) {
DelayData data = delayQueue.take();
System.out.println("id:"+data.getId()+"，数据:"+data.getName()+"被移除，当前时间:"+System.currentTimeMillis());
}
}
}
``````

``````开始计时时间:1583333583216
id:2，数据:C被移除，当前时间:1583333585216
id:5，数据:A被移除，当前时间:1583333588216
id:8，数据:B被移除，当前时间:1583333591216
``````

0
0 收藏

0 评论
0 收藏
0