news 2026/7/23 13:26:42

阻塞队列的使用和实现

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
阻塞队列的使用和实现

阻塞队列是一种特殊的队列,其遵循“先入先出”的原则。

阻塞队列也是一种线程安全的数据结构,具有以下特性:

  • 队列为满,入队列产生阻塞,直至其他线程从队列中取走元素
  • 队列为空,出队列产生阻塞,直至其他线程往队列中插入元素

“生产者消费者模型”是阻塞队列的一个典型应用场景,该模型也是一个典型的开发模型。

生产者消费者模型

生产者消费者模型就是通过一个中间容器来解决生产者和消费者之间的强耦合问题。

这个中间容器通过阻塞队列实现,从而使生产者和消费者之间不进行直接通讯。

阻塞队列的作用:

  1. 阻塞队列相当于一个缓冲区,平衡了生产者和消费者的处理能力(削峰填谷)
  2. 阻塞队列使生产者和消费者之间解耦

阻塞队列的缺点:

  1. 引入队列以后,代码整体结构变复杂
  2. 程序执行效率有所影响

阻塞队列的使用

Java的标准库中,提供了现成的阻塞队列。

  • BlockingQueue是一个接口,真正实现的类有:LinkedBlockingQueue(链表实现),ArrayBlockingQueue(数组实现),PriorityBlockingQueue(堆实现)等等。
  • 队列的出操作是poll,入队列操作是offer,但是阻塞队列使用的分别时take和put,这两个方法是带有阻塞功能的出入队列操作。
public class demo1 { public static void main(String[] args) { //创建阻塞队列 BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(1000); //生产者线程 Thread producer = new Thread(() -> { int n = 0; while (true) { try { queue.put(n); System.out.println("生产元素 " + n); n++; } catch (InterruptedException e) { throw new RuntimeException(e); } } }, "procducer"); //消费者线程 Thread consumer = new Thread(() -> { while(true){ try { int n = queue.take(); System.out.println("消费元素 " + n); } catch (InterruptedException e) { throw new RuntimeException(e); } } }, "consumer"); producer.start(); consumer.start(); } }

阻塞队列的简单实现

  • 使用“循环队列”实现
  • 使用synchronized加锁保证线程安全
  • 注意这里的wait()搭配while使用,而不能搭配if使用,是由于notifyAll会将所有的wait唤醒,只有其中一个线程会put,而等到其他被唤醒的线程拿到锁之后,还需要确认一下容量是否已满,满的话还需要阻塞等待。
class BlockingQueue { private int[] item = new int[1000]; private volatile int head = 0; private volatile int tail = 0; private volatile int size = 0; //生产元素 public void put(int value) throws InterruptedException { synchronized (this) { while (size == item.length) { this.wait(); } item[tail] = value; tail++; if (tail == item.length) { tail = 0; } size++; notifyAll(); } } //消费元素 public int take() throws InterruptedException { synchronized (this) { while (size == 0) { this.wait(); } int ret = item[head]; head++; if(head == item.length){ head = 0; } size--; notifyAll(); return ret; } } //获取内部属性 public synchronized int getSize() { return size; } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/22 4:51:35

Topit:3步解决Mac窗口遮挡难题,让你的关键内容始终置顶

Topit&#xff1a;3步解决Mac窗口遮挡难题&#xff0c;让你的关键内容始终置顶 【免费下载链接】Topit Pin any window to the top of your screen / 在Mac上将你的任何窗口强制置顶 项目地址: https://gitcode.com/gh_mirrors/to/Topit 在日常工作中&#xff0c;你是否…

作者头像 李华
网站建设 2026/7/22 12:54:10

Topit窗口置顶:macOS多任务管理的终极解决方案

Topit窗口置顶&#xff1a;macOS多任务管理的终极解决方案 【免费下载链接】Topit Pin any window to the top of your screen / 在Mac上将你的任何窗口强制置顶 项目地址: https://gitcode.com/gh_mirrors/to/Topit 在当今快节奏的数字工作环境中&#xff0c;macOS用户…

作者头像 李华
网站建设 2026/7/21 18:03:08

LangFlow支持的LangChain组件清单及使用示例

LangFlow支持的LangChain组件清单及使用示例 在大语言模型&#xff08;LLM&#xff09;迅速渗透各行各业的今天&#xff0c;越来越多团队希望快速构建智能问答、知识库助手或自动化代理系统。然而&#xff0c;直接基于 LangChain 编写代码往往意味着要处理复杂的链式结构、提示…

作者头像 李华
网站建设 2026/7/21 22:08:02

如何快速掌握Topit:Mac窗口置顶终极指南

如何快速掌握Topit&#xff1a;Mac窗口置顶终极指南 【免费下载链接】Topit Pin any window to the top of your screen / 在Mac上将你的任何窗口强制置顶 项目地址: https://gitcode.com/gh_mirrors/to/Topit 在当今多任务处理的时代&#xff0c;你是否经常遇到关键窗口…

作者头像 李华
网站建设 2026/7/23 1:41:10

TrollInstallerX 终极下载指南:如何轻松应对安全拦截问题

TrollInstallerX 终极下载指南&#xff1a;如何轻松应对安全拦截问题 【免费下载链接】TrollInstallerX A TrollStore installer for iOS 14.0 - 16.6.1 项目地址: https://gitcode.com/gh_mirrors/tr/TrollInstallerX TrollInstallerX 是一款专为 iOS 14.0 至 16.6.1 系…

作者头像 李华