阻塞队列:
阻塞队列是在原来的队列(先进先出)的基础上,做出的扩充。
主要特征如下:
1、线程安全
2、具有阻塞特性:
(1).如果队列为空,此时出队列操作就会阻塞,一直阻塞到其他线程在队列中添加新的元素。
(2).如果队列满了,此时入队列操作就会进行阻塞,一直阻塞到其他线程从队列中取走元素为止。
基于阻塞队列,最大的应用场景是"生产者消费者模型",在后端开发中"分布式系统",不是一台服务器解决所有的问题,而是分成了多台服务器,服务器之间相互调用。
例如:
使用生产者消费者模型主要有两个好处:
(1).服务器之间解耦合
阻塞队列是一种数据结构,由于这种数据结构非常好用,以至于将该数据结构单独单独封装成一个服务器程序,在单独的服务器上进行部署。
此时阻塞队列就有了一个新的名字——(Message Queue,MQ)。
(2).通过阻塞队列可以起到一个"削峰填谷"的作用,当请求量剧增的时候,可以有效的保护下游服务器,不会被请求冲垮!
代价:
当然消息队列是可以帮助服务器优化削峰填谷和解耦合,但是要用阻塞队列也有一定的代价的:
1、需要更多的硬件成本,来部署这样的消息队列
2、A和B之间的通信时间会变长。
BlockingQueue接口:
Java标准库中就有这样的阻塞队列接口——BlockingQueue
实现BlockingQueue接口的类主要使用的有以下三种:
分别是基于数组实现、基于链表实现、基于优先级队列(堆)实现。
当然这些类都是有一些带有参数的构造方法:
如果有一个参数的里面的表示的是容量大小。
其次就是插入元素和取元素:
首先该类继承了Queue,也就是Queue中的offer()和poll()方法都可以使用,但是这两个方法就相当于是普通方法不涉及带有阻塞功能。
BlockingQueue提供了专门的方法,put()和take()方法,可能产生阻塞,可以被interrupt方法唤醒!接下俩我们就使用标准库中的类和方法使用一下阻塞队列:
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(5);
Thread t1 = new Thread(()-> {
for (int i = 0; i < 6; i++) {
try {
blockingQueue.put(i+1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
Thread t2 = new Thread(()->{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
for (int i = 0; i < 7; i++) {
try {
System.out.println(blockingQueue.take());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
}
自己实现一个BlockingQueue:
要想真真意义上学懂BlockingQueue,自己手动搓一个才有意义,我们基于数组实现:
public class MyBlockingQueue {
private String[] array;
private int count;
//做循环队列需要
private int tail;
private int head;
public MyBlockingQueue(int capacity) {
array = new String[capacity];
}
public void put(String s) throws InterruptedException {
//循环队列
synchronized (this) {
if (array.length == count) {
this.wait();
}
array[tail] = s;
tail++;
if (tail >= array.length) {
tail = 0;
}
count++;
this.notify();
}
}
public String take() throws InterruptedException {
String ret = "";
synchronized (this) {
if (count == 0) {
this.wait();
}
ret = array[head];
head++;
if (head >= array.length) {
head = 0;
}
count--;
this.notify();
}
return ret;
}
}
此时代码写的已经差不多了,但是需要考虑一些场景,确保线程安全:
1、读取安全问题,这里发现有可能会造成一个线程在修改一个线程在读取操作,也即是内存可见性问题,所以为防止该问题发生此时在变量前面加volatile修饰。
2、 wait不仅仅会被notify唤醒,也可能会被interrupt唤醒,当然这是在当被interrupt唤醒后不报异常的情况,所以此时当被唤醒之后需要再次确认一下是否被真的唤醒了,所以此时使用wait的时候搭配while。
public class MyBlockingQueue {
private String[] array;
private volatile int count;
//做循环队列需要
private volatile int tail;
private volatile int head;
public MyBlockingQueue(int capacity) {
array = new String[capacity];
}
public void put(String s) throws InterruptedException {
//循环队列
synchronized (this) {
while (array.length == count) {
this.wait();
}
array[tail] = s;
tail++;
if (tail >= array.length) {
tail = 0;
}
count++;
this.notify();
}
}
public String take() throws InterruptedException {
String ret = "";
synchronized (this) {
while (count == 0) {
this.wait();
}
ret = array[head];
head++;
if (head >= array.length) {
head = 0;
}
count--;
this.notify();
}
return ret;
}
}