美文网首页
07 | 队列:队列在线程池等有限资源池中的应用

07 | 队列:队列在线程池等有限资源池中的应用

作者: 雪燃归来 | 来源:发表于2020-05-18 08:55 被阅读0次
image.png

一、什么是队列?

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

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


image.png

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

二、如何实现队列?

1.队列API

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

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


image.png
image.png
// 使用数组实现队列
class ArrayQueue {
  // 数组: items, 数组大小:n
  // 申请一个大小为capacity的数组
  constructor(capacity) {
    this.items = new Array(capacity);
    this.n = capacity;
    this.head = 0;
    this.tail = 0;
  }

  // 入队
  enqueue(item) {
    // 如果tail === n 表示队列已经满了
    if (this.tail === this.n) return false;
    this.items[this.tail] = item;
    this.tail++;
    return this;
  }

  // 出队
  dequeue() {
    // 如果head === tail 表示队列已经为空了
    if (this.head === this.tail) return false;
    let ret = this.items[this.head];
    ++this.head;
    return ret;
  }

  // 显示当前队列的存储情况
  display() {
    let _arr = [];
    for (let i = this.head; i < this.tail; i++) {
      _arr.push(this.items[i]);
    }
    return _arr;
  }
}

let array = new ArrayQueue(3);
array.enqueue("1").enqueue("2").enqueue("3");
console.log(array.display()); // ["1", "2", "3"]
console.log(array.dequeue()); //1
console.log(array.display()); //["2", "3"]

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


image.png
/**
 * 基于链表实现队列
 */
class Node {
  constructor(element) {
    this.element = element;
    this.next = null;
  }
}

class QueueBaseOnLinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
  }

  enqueue(value) {
    if (this.head === null) {
      this.head = new Node(value);
      this.tail = this.head;
    } else {
      this.tail.next = new Node(value);
      this.tail = this.tail.next;
    }
  }

  dequeue() {
    if (this.head !== null) {
      let element = this.head.element;
      this.head = this.head.next;
      return element;
    } else {
      return -1;
    }
  }
}

//test
const newQueue = new QueueBaseOnLinkedList();
// insert
newQueue.enqueue(1);
newQueue.enqueue(2);
newQueue.enqueue(3);
// get
let res = 0;
console.log("-------获取dequeue元素------");
while (res !== -1) {
  res = newQueue.dequeue();
  console.log(res);
}

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

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

1.阻塞队列

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

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


image.png

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


image.png

2.并发队列

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

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

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

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

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

相关文章

网友评论

      本文标题:07 | 队列:队列在线程池等有限资源池中的应用

      本文链接:https://www.haomeiwen.com/subject/xkeyohtx.html