05、|队列(queue):队列在线程池等有限资源池中的应用
5.1 如何理解“队列”?
- 先进者先出,这就是典型的“队列”
- 最基本的操作也是两个:
- 入队 enqueue(),放一个数据到队列尾部;
- 出队 dequeue(),从队列头部取一个元素。
- 实现:
- 用数组实现的队列叫作顺序队列;
- 用链表实现的队列叫作链式队列。
- 队列需要两个指针:
- 一个是 head 指针,指向队头;
- 一个是 tail 指针,指向队尾。
5.2 基于数组实现的队列
用队列实现排队请求,基于数组实现的有界队列(bounded queue),队列的大小有限,所以线程池中排队的请求超过队列大小时,接下来的请求就会被拒绝,这种方式对响应时间敏感的系统来说,就相对更加合理。
不过,设置一个合理的队列大小,也是非常有讲究的。队列太大导致等待的请求太多,队列太小会导致无法充分利用系统资源、发挥最大性能。
public class ArrayQueue {
private String[] items;
private int n = 0;
private int head = 0;
private int tail = 0;
public ArrayQueue(int capacity){
items = new String[capacity];
n = capacity;
}
//入队
public boolean enqueue(String item){
if (tail == n) {
if (head == 0){
//队满
return false;
}
//经过多次入队、出队操作,虽然队尾到了申请数组的最末端
//但是队头之前有了空闲空间,我们进行数据搬移
for (int i = head; i < tail; i++) {
items[i - head] = items[i];
}
tail -= head;
head = 0;
}
items[tail] = item;
tail ++;
return true;
}
public String dequeue(){
if (head == tail){
return null;
}
String ret = items[head];
head ++;
return ret;
}
public void printAll(){
for (int i = head; i < tail; i++) {
System.out.println(items[i] + " ");
}
System.out.println();
}
}
5.3 基于链表实现的队列
基于链表的实现方式,可以实现一个支持无限排队的无界队列(unbounded queue),但是可能会导致过多的请求排队等待,请求处理的响应时间过长。所以,针对响应时间比较敏感的系统,基于链表实现的无限排队的线程池是不合适的。
public class QueueBasedOnLinkedList {
private Node head = null;
private Node tail = null;
public void enqueue(String value){
Node node = new Node(value, null);
if (tail == null){
head = node;
tail = node;
}else {
tail.next = node;
}
}
public String dequeue(){
if (head == null){
return null;
}
String value = head.data;
head = head.next;
if (head == null){
tail = null;
}
return value;
}
public void printAll(){
Node p = head;
while (p != null){
System.out.println(p.data + " ");
p = p.next;
}
System.out.println();
}
private static class Node{
private String data;
private Node next;
public Node(String data, Node next) {
this.data = data;
this.next = next;
}
public String getData() {
return data;
}
}
}
5.4 循环队列
- 循环队列可以避免数组实现时的数据搬移
- 队空队满的判断
- 队列为空的判断条件是 head == tail
- 队满时,(tail+1)%n=head。
- 循环队列会浪费一个数组的存储空间。
![循环队列](http://image.yucode.cn/%E5%BE%AA%E7%8E%AF%E9%98%9F%E5%88%97.jpg)
public class CircularQueue {
private String[] items;
private int n = 0;
private int head = 0;
private int tail = 0;
public CircularQueue(int capacity) {
items = new String[capacity];
n = capacity;
}
public boolean enqueue(String item){
if ((tail + 1) % n == head){
return false;
}
items[tail] = item;
tail = (tail + 1) % n;
return true;
}
public String dequeue(){
if (head == tail){
return null;
}
String ret = items[head];
head = (head + 1) % n;
return ret;
}
public void printAll(){
if (n == 0) return;
for (int i = head; i % n != tail; i = (i + 1) % n){
System.out.println(items[i] + " ");
}
System.out.println();
}
}
5.5阻塞队列和并发队列
- 阻塞队列其实就是在队列基础上增加了阻塞操作。简单来说,就是在队列为空的时候,从队头取数据会被阻塞。因为此时还没有数据可取,直到队列中有了数据才能返回;如果队列已经满了,那么插入数据的操作就会被阻塞,直到队列中有空闲位置后再插入数据,然后再返回。我们可以使用阻塞队列,轻松实现一个“生产者 - 消费者模型”!
- 线程安全的队列我们叫作并发队列。最简单直接的实现方式是直接在 enqueue()、dequeue() 方法上加锁,但是锁粒度大并发度会比较低,同一时刻仅允许一个存或者取操作。实际上,基于数组的循环队列,利用 CAS(:compare and swap 比较并交换。该操作通过将内存中的值与指定数据进行比较,当数值一样时将内存中的数据替换为新的值。) 原子操作,可以实现非常高效的并发队列。
——学习王争老师数据结构与算法之美课后总结