一、什么是队列?
1、满足先进先出的特性FIFO
2、二个基本操作:入队 enqueue(),放一个数据到队列尾部;出队 dequeue().从队列头部取个数据
3、和 栈一样也是操作受限的线性表
二、如何实现队列?
1、数组实现-顺序队列
对应栈,我们只需要一个栈顶指针。但是队列需要两个指针:一个head指针,指向队头;一个tail指针,指向队尾
当 a、b、c、d 依次入队之后,队列中的 head 指针指向下标位0的位置,tail指向下标为4的位置
当我们调用两次出队操作之后,队列中 head 指针指向下标为2的位置,tail仍然指向下标为4的位置
这个基于数组实现的队列可以扩容
package dataStruct.queue;
//队列用数组实现
public class QueueOfArray {
private Object[] data;
private int head; // 头指针
private int tail; // 尾指针
// 队列初始化容量
private int n;
public QueueOfArray(int initCapicity) {
this.data = new Object[initCapicity];
this.n = initCapicity;
this.head = 0;
this.tail = 0;
}
public boolean enqueue(Object ele) {
// tail == n 表示队列末尾没有空间了
if (tail == n) {
// tail==n && head==0 表明队列已满
if (head == 0) {
return false;
}
// 数据搬移
for (int i = head; i < tail; i++) {
data[i - head] = data[i];
}
// 搬移后更新head和tail
this.head = 0;
this.tail = tail - head;
}
data[tail] = ele;
tail++;
return true;
}
public Object dequeue() {
// head==tail表示队列为空
if (tail == head) {
return null;
}
Object ret = data[head];
head++;
return ret;
}
}
2、链表实现-链式队列
package dataStruct.queue;
//队列用链表实现
public class QueueOfSinglyLinked {
Node head; // 头指针
Node tail; // 尾指针
public QueueOfSinglyLinked() {
this.head = null;
this.tail = null;
}
class Node {
Object data;
Node next;
public Node(Object data, Node p) {
this.data = data;
this.next = p;
}
}
public boolean enqueuq(Object ele) {
Node newNode = new Node(ele, null);
// tala==null说明空链表
if (tail == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
return true;
}
public Object dequeue() {
// head==null说明空表
if (head == null) {
return false;
}
Object ret = head.data;
head = head.next;
// 移动头指针后为Null,说明队列为空了
if (head == null) {
tail = null;
}
return ret;
}
public void printAll() {
Node p = head;
while (p != null) {
System.out.print(p.data);
p = p.next;
}
}
}
3、循环队列-基于数组
用数组实现队列的时候,当tail==n && head = 0 表示队列已满,会有数据搬移操作,性能受到影响。为了避免数据搬移,就引入了循环队列
如图,队列初始化容量为8,当前head=4,tail==7。当一个新元素入队时,放入下标为7的位置,不把tail更新为8,而是将其在环中后移一位,到下标为0位置。再有元素b入队,将其放入下标为0的位置,然后tail+1更新为1。如下图:
注意:主要判断好队空和队满的判断条件
规律:
队满:(tail+1)%n=head
队空:head==tail
就图中画的队满的情况,tail=3,head=4,n=8,规律是(3+1)%8=4
package dataStruct.queue;
//基于数组实现的循环队列
public class CircleQueue {
private Object[] datas;
private int head = 0; // 队头下标
private int tail = 0;// 队尾下标
int n = 0;// 队列初始化容量
public CircleQueue(int initCapicity) {
this.datas = new Object[initCapicity];
this.n = initCapicity;
}
// 入队
public boolean enqueue(Object ele) {
// 队列已满
if ((tail + 1) % n == head)
return false;
datas[tail] = ele;
tail = (tail + 1) % n;
return true;
}
// 出队
public Object dequeue() {
// 队列为空
if (head == tail)
return -1;
Object result = datas[head];
head = (head+1) % n;
return result;
}
}
三、队列的应用
1、阻塞队列
1)就是在队列的基础增加了阻塞的操作。
简单来说就是在队列为空的时候,从队头取数据会被阻塞。因为此时无数据可取,知道队列中有了数据才能返回;如果队列已满,那么插入数据的操作就会被阻塞,知道队列中有了空闲位置后再插入数据,然后返回。
2)从上面的定义可以看出这就是一个“生产者-消费者模型”。这种基于阻塞队列实现的“生产者-消费者模型”可以有效地协调生产和消费的速度。当“生产者”生产数据的速度过快,“消费者”来不及消费时,存储数据的队列很快就会满了,这时生产者就阻塞等待,直到“消费者”消费了数据,“生产者”才会被唤醒继续生产。不仅如此,基于阻塞队列,我们还可以通过协调“生产者”和“消费者”的个数,来提高数据处理效率,比如配置几个消费者,来应对一个生产者。
2、并发队列
1)在多线程的情况下,会有多个线程同时操作队列,这时就会存在线程安全问题。能够有效解决线程安全问题的队列就称为并发队列。
2)并发队列简单的实现就是在enqueue()、dequeue()方法上加锁,但是锁粒度大并发度会比较低,同一时刻仅允许一个存或取操作。
3)实际上,基于数组的循环队列利用CAS原子操作,可以实现非常高效的并发队列。这也是循环队列比链式队列应用更加广泛的原因
线程池没有空闲线程时,新的任务请求线程资源时,线程池该如何处理?各种策略如何实现?
第一种非阻塞的处理方式:直接拒接任务请求
第二种时阻塞的处理方式:将请求排队,等到有空闲线程时,取出队列的请求继续处理,那如何存储排队的请求呢?
队列有基于数组和链表的实现,这二种方式对于队列请求有什么区别?
基于链表:支持无限排队无界队列,但可能导致过多的排队请求,请求响应时间过长,针对响应时间比较敏感的系统不适合
基于数组:队列的大小有限,所以线程池中排队的请求超过队列大小时,接下来的请求被拒绝,比较适合对响应时间敏感的系统
除了前面讲到队列应用在线程池请求排队的场景之外,队列可以应用在任何有限资源池中,用于排队请求,比如数据库连接池
等。实际上,对于大部分资源有限的场景,当没有空闲资源时,基本上都可以通过“队列”这种数据结构来实现请求排队。
四、思考
1.除了线程池这种池结构会用到队列排队请求,还有哪些类似线程池结构或者场景中会用到队列的排队请求呢?
2.今天讲到并发队列,关于如何实现无锁的并发队列,网上有很多讨论。对这个问题,你怎么看?
答 :
1.分布式应用中的消息队列,也是一种队列结构
2.考虑使用CAS实现无锁队列,则在入队前,获取tail位置,入队时比较tail是否发生变化,如果否,则允许入队,反之,本次入队失败。出队则是获取head位置,进行cas。(compare and swap)