1010:队列

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_1018944104/article/details/83025908

总结

一、什么是队列?

1.先进者先出,这就是典型的“队列”结构。

2.支持两个操作:入队enqueue(),放一个数据到队尾;出队dequeue(),从队头取一个元素。

3.所以,和栈一样,队列也是一种操作受限的线性表。

二、如何实现队列?

1.队列API

public interface Queue<T> {
    public void enqueue(T item); //入队
    public T dequeue(); //出队
    public int size(); //统计元素数量
    public boolean isNull(); //是否为空
}

2.数组实现(顺序队列)

public class ArrayQueue {
	//存储数据的数组
	private String[] items;
	//记录数组容量
	private int n;
	private int size;
	//head记录队头索引,tail记录队尾索引
	private int head = 0;
	private int tail = 0;
	//申请一个指定容量的队列
	public ArrayQueue(int capacity){
		items = new String[capacity];
		n = capacity;
	}
	/*
	 * 入队:
	 * 1.堆满的时,入队失败
	 * 	1.1频繁出入队,造成数组使用不连续
	 * 	1.2在入队的时候,集中触发进行数据搬移
	 * 2.在末尾插入数据,注意tail指向队尾元素的索引+1
	 */
	public boolean enqueue(String item){
		//表示队满
		if(head == 0 && tail == n)
			return false;
		//表示需要数据搬移
		else if(head != 0 && tail == n){
			for (int i = head; i < tail; i++) {
				items[i-head] = items[i];
			}
			head = 0;
			tail = tail - head;
		}
		//将数据加入队列
		items[tail++] = item;
		size++;
		return true;
	}
	//出队:1.队空时,出队失败;2.出队,head索引+1
	public String dequeue(){
		String res = null;
		if(head == tail) return res;
		res = items[head++];
		size--;
		return res;
	}
}

3.链表实现(链式队列)

public class LinkedQueue {
	//定义一个节点类
	private class Node{
		String value;
		Node next;
	}
	//记录队列元素个数
	private int size = 0;
	//head指向队头结点,tail指向队尾节点
	private Node head;
	private Node tail;
	//申请一个队列
	public LinkedQueue(){}
	//入队
	public boolean enqueue(String item){
		Node newNode = new Node();
		newNode.value = item;
		if (size == 0) head = newNode; 
		else tail.next = newNode;
		tail = newNode;
		size++;
		return true;
	}
	//出队
	public String dequeue(){
		String res = null;
		if(size == 0) return res;
		if(size == 1) tail = null;
		res = head.value;
		head = head.next;
		size--;
		return res;
	}
}

4.循环队列(基于数组)

public class LoopArrayQueue {
	//存储数据的数组
	private String[] items;
	//记录数组容量
	private int n;
	private int size = 0;
	//head记录队头索引,tail记录队尾索引
	private int head = 0;
	private int tail = 0;
	//申请一个指定容量的队列
	public LoopArrayQueue(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;
		size++;
		return true;
	}
	//出队:关键在于队空的条件
	public String dequeue(){
		String res = null;
		if(head == tail) return res;
		res = items[head];
		head = (head + 1) % n;
		size--;
		return res;
	}
}

三、队列有哪些常见的应用?

1.阻塞队列

1)在队列的基础上增加阻塞操作,就成了阻塞队列。

2)阻塞队列就是在队列为空的时候,从队头取数据会被阻塞,因为此时还没有数据可取,直到队列中有了数据才能返回;如果队列已经满了,那么插入数据的操作就会被阻塞,直到队列中有空闲位置后再插入数据,然后在返回。

3)从上面的定义可以看出这就是一个“生产者-消费者模型”。这种基于阻塞队列实现的“生产者-消费者模型”可以有效地协调生产和消费的速度。当“生产者”生产数据的速度过快,“消费者”来不及消费时,存储数据的队列很快就会满了,这时生产者就阻塞等待,直到“消费者”消费了数据,“生产者”才会被唤醒继续生产。不仅如此,基于阻塞队列,我们还可以通过协调“生产者”和“消费者”的个数,来提高数据处理效率,比如配置几个消费者,来应对一个生产者。

2.并发队列

1)在多线程的情况下,会有多个线程同时操作队列,这时就会存在线程安全问题。能够有效解决线程安全问题的队列就称为并发队列。

2)并发队列简单的实现就是在enqueue()、dequeue()方法上加锁,但是锁粒度大并发度会比较低,同一时刻仅允许一个存或取操作。

3)实际上,基于数组的循环队列利用CAS原子操作,可以实现非常高效的并发队列。这也是循环队列比链式队列应用更加广泛的原因。

3.线程池资源枯竭是的处理

在资源有限的场景,当没有空闲资源时,基本上都可以通过“队列”这种数据结构来实现请求排队。

四、思考

1.除了线程池这种池结构会用到队列排队请求,还有哪些类似线程池结构或者场景中会用到队列的排队请求呢?

2.今天讲到并发队列,关于如何实现无锁的并发队列,网上有很多讨论。对这个问题,你怎么看?

猜你喜欢

转载自blog.csdn.net/qq_1018944104/article/details/83025908