阻塞式队列–BlockingQueue
遵循先进先出(FIFO)的原则。阻塞式队列本身使用的时候是需要指定界限。
- add/offer/put, remove/poll/take
ArrayBlockingQueue–阻塞式顺序队列–底层依靠数组来存储数据,并且在使用的时候一定要指定容量
public class ArrayBlockingQueueDemo {
public static void main(String[] args) throws InterruptedException {
// 这个队列在创建的时候需要指定容量,容量在指定之后不可变
ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(5);
// 添加队列
queue.add("a");
queue.add("b");
queue.add("c");
queue.add("d");
queue.add("e");
// 如果队列已满,则抛出异常 - IllegalStateException
// queue.add("a");
// 返回值标记元素是否成功添加到队列里面
// 如果队列已满,则返回false
// boolean b = queue.offer("b");
// System.out.println(b);
// 如果队列已满,会产生阻塞 --- 直到这个队列中有元素被取出,才会放开阻塞
// queue.put("c");
// 定时阻塞
// 在3s之内如果有元素被取出,那么元素就会添加到队列中
// 如果3s之后队列依然是满的,那么返回false表示添加失败
boolean b = queue.offer("d", 3000, TimeUnit.MILLISECONDS);
System.out.println(b);
System.out.println(queue);
}
}
public class ArrayBlockingQueueDemo2 {
public static void main(String[] args) throws InterruptedException {
ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(5);
// 队列为空,抛出异常:NoSuchElementException
// String str = queue.remove();
// 队列为空,返回null
// String str = queue.poll();
// 队列为空,产生阻塞 - 直到有其他线程向队列中添加元素,才会放开阻塞
// String str = queue.take();
// 定时阻塞
String str = queue.poll(3, TimeUnit.SECONDS);
System.out.println(str);
}
}
LinkedBlockingQueue - 阻塞式链式队列 - 底层依靠节点来存储数据。在使用的时候可以指定容量,也可以不指定。但是如果不指定,那么默认容量是Integer.MAX_VALUE->2^31-1
使用方法和上面相同
PriorityBlockingQueue - 具有优先级的阻塞式队列 - 要求存储的元素所对应的类必须实现Comparable,会在元素一一取出的时候进行自然排序。在迭代的时候不保证元素的排序
public class PriorityBlockingQueueDemo {
public static void main(String[] args) throws InterruptedException {
PriorityBlockingQueue<String> queue = new PriorityBlockingQueue<>();
queue.add("f");
queue.add("e");
queue.add("a");
queue.add("u");
queue.add("l");
// System.out.println(queue);
for(int i = 0; i < 5; i++){
System.out.println(queue.take());
}
}
}
public class PriorityBlockingQueueDemo2 {
public static void main(String[] args) throws InterruptedException {
PriorityBlockingQueue<Student> queue = new PriorityBlockingQueue<>();
queue.put(new Student("程咬金", 15, 59));
queue.put(new Student("秦琼", 18, 89));
queue.put(new Student("王世充", 80, 48));
queue.put(new Student("李元霸", 12, 90));
for (Student s : queue) {
System.out.println(s);
}
for(int i = 0; i < 4; i++){
System.out.println(queue.take());
}
}
}
class Student implements Comparable{
private String name;
private int age;
private int score;
public Student(String name, int age, int score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
}
// @Override
// 指定比较规则
// 如果返回值>0,那么this就要排到o的后边
// 如果返回值<0,那么this就要排到o的前边
// public int compareTo(Student o) {
// return this.age - o.age;
// }
@Override
public int compareTo(Student o) {
return o.score - this.score;
}
}
SynchronousQueue - 同步队列 - 只允许存储1个元素—相当于一个点