队列分类:普通队列,优先级队列,阻塞队列,消息队列
阻塞队列
什么是阻塞队列以及消息队列,我们引出以下场景:在请求和响应量很大的情况下,如果服务器硬件设备不支持的情况下,服务器就会卡住,造成难以想象的后果,此时我们可以使用”中转站“,来承载A-B之间的请求和响应,如下图
我们刚才所说的第一种情况就是A_B直接进行交互,此时AB之间的交互如果很频繁就会使B服务器承载不了,如果是操作数据库的那种比较重量级的操作就会使服务器卡顿,但是如果引入消息队列,也就是C,可以有效地避免这种情况,此时我们说一下该操作的优点:
阻塞队列(生产者消费者模式模型)的优点
1.解耦合:耦合是指两个程序如果你中有我,我中有你,相互之间一动而牵制其他程序,则说明他们之间连接在了一起,这种情况叫做耦合太高;那么消息队列是如何使程序(服务器)之间耦合呢?在客户端给A服务器发送请求时,先把请求放在消息队列C中,此时,B服务器就不怕请求太多而导致卡顿,B服务器可以按照自己的节奏来进行操作,此时就解耦合了,使得A,B服务器只用关心自己所在的那一部分,只用交互自己的程序,此时也就说消息队列的优点是解耦合
2.削峰填谷:什么是削峰填谷,这种作用相当于三峡大坝的作用,在夏季雨多时,在上游把水存储起来,等到旱季时,把水在放下来,此时两种情况都达到了平均值,也就达到了农业不会被水所耽误的情况,达到一种平均的情况,如下图,就是一种削峰填谷的作用,在AB服务器之间达到均衡,都能在自己的水平范围内将问题解决,此时阻塞队列C充当一个缓冲区的作用
以上的模型就叫做生产者消费者模型,上述两个优点也是因为消息队列和两个服务器共同起到的作用,那么我们来介绍
消息队列
消息队列在应用开发时很重要,一般会单独部署一个服务器来进行开发
消息队列在Java中是以阻塞队列(BlockingQueue)的形式出现
但消息队列和阻塞队列不是一种东西,消息队列用的是topic方法,举一个医院的例子,在超声科B超这一项上,有检查肾的,有检查产检的,有检查心脏的,医生说来一个检查肾的,那么检查肾的就会过去,这一个topic就代表着不同的类型
阻塞队列的子类
其中有顺序表队列,优先级队列,链表队列等队列,其中阻塞队列有以上类,阻塞队列也是队列,也遵循先进先出,后进后出的原则,但是其有一个特点,就是在队列为空时无法出队,在队列为满时无法入队,使用wait和notify方法来进行构建类,这是一个线程安全的队列,正常队列是使用poll和offer方法,但是阻塞队列使用的是take和put方法
演示阻塞队列的两种线程安全的方法
class Test {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<>();
blockingQueue.put(1);//入队
blockingQueue.take();//出队
}
}
class Test {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<>(2);//写两个容量
blockingQueue.put(1);
blockingQueue.put(1);
blockingQueue.put(1);//写三个入队
}
}
此时主线程就会堵塞
class Test {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<>(2);
blockingQueue.put(1);
blockingQueue.put(1);
blockingQueue.take();
blockingQueue.take();
blockingQueue.take();
}//此时写两个入队,三个出队,也会进入堵塞状态,等待下一个线程来拯救
}
offer和poll都是单线程方法,是为了实现BlockingQueue接口的方法所以才实现的,put和take则不是,是LinkedBlockingQueue自己的类所实现的方法,是多线程方法,更线程安全
实现阻塞队列
我们自己来实现一个阻塞队列,其中主要包含线程安全的put和take的方法,我们使用循环队列来实现,在take方法里有%的方法可以参考循环队列实现的三种解决尾与头相等的方法
public class MBQ {
private int[] elem;
private volatile int head = 0;
private volatile int tail = 0;
private volatile int size = 0;
//Object locker = new Object();
private static final int DEFAULT_CAPACITY = 10;
public MBQ(int capacity) {
elem = new int[DEFAULT_CAPACITY];
}
public void put(int val) throws InterruptedException {
synchronized (this) {
if (size > elem.length) {
this.wait();//此时是为了防止队列满了就不能入队列,线程堵塞
}
elem[tail] = val;
tail++;
size++;
if (tail == elem.length) {
tail = 0;
}
this.notify();//这里的唤醒是为了把take方法唤醒,放置多个线程全部wait
}
}
public int take() throws InterruptedException {
synchronized (this) {
if (size <= 0) {//此时是为了防止队列为空不能出队列,直接线程堵塞
this.wait();
}
head++;
if(head>= elem.length) {
head=0;
}
tail = (tail + 1) % elem.length;
size--;
this.notify();//这里的唤醒是为了把put方法的wait唤醒
return elem[head];
}
}
}
改进阻塞队列,同时查看wait方法文档
我们这里可以改进一下,我们查看wait方法的文档
其中那段代码的意思是要把wait和while循环一起用,虽然我们这里的代码也没有问题,但是我们遵循规则,是为了防止多线程一起判断的时候会判断出错,我们使用while循环来改进
public void put(int val) throws InterruptedException {
synchronized (this) {
while (size > elem.length) {
this.wait();
}
elem[tail] = val;
tail++;
size++;
if (tail == elem.length) {
tail = 0;
}
this.notify();
}
}
public int take() throws InterruptedException {
synchronized (this) {
while (size <= 0) {
this.wait();
}
head++;
if (head >= elem.length) {
head = 0;
}
tail = (tail + 1) % elem.length;//这种方式还可以写成tail=tail+1,但要考虑tail和head相等的情况
size--;
this.notify();
return elem[head];
}
}
此时不会出现多线程安全的问题
生产者消费者模型是什么?顾名思义,是多线程,有线程生产,有线程消耗
class ProducerAndConsumer {
static Integer count = 0;
//我们使用两个线程来表示生产者和消费者
public static void main(String[] args) {
BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue(1000);
Thread t1 = new Thread(() -> {
while (true) {
try {
blockingQueue.put(count);
System.out.println("shengchanle"+count);
count++;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
Thread t2 = new Thread(() -> {
while(true) {
try {
int t = blockingQueue.take();
System.out.println("t消费了"+count);
Thread.sleep(1000);
count--;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
t1.start();
t2.start();
}
}
此时count会稳定在1000左右这是一个典型的场景
综上,生产者消费者模式是一个很实用的模式,并且优点很多且实用,是我们未来工作也要使用的模式