Java中常用阻塞队列的问题是什么
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,本篇内容主要讲解"Java中常用阻塞队列的问题是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Java中常用阻塞队列的问题是什么"吧!Java常用阻塞
千家信息网最后更新 2025年11月08日Java中常用阻塞队列的问题是什么
本篇内容主要讲解"Java中常用阻塞队列的问题是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Java中常用阻塞队列的问题是什么"吧!
Java常用阻塞队列
ArrayBlockingQueue
内部由一个固定长度的数组来实现阻塞队列
/** The queued items */final Object[] items;/** items index for next take, poll, peek or remove */int takeIndex;/** items index for next put, offer, or add */int putIndex;public ArrayBlockingQueue(int capacity, boolean fair) { if (capacity <= 0) throw new IllegalArgumentException(); /** 定长数组 */ this.items = new Object[capacity]; lock = new ReentrantLock(fair); notEmpty = lock.newCondition(); notFull = lock.newCondition();}提供了两个入队操作方法,offer()和put()
offer方法不会阻塞,但有返回值,如果队列满了,那么直接返回false,否则插入数据并返回true。
/** * Inserts the specified element at the tail of this queue if it is * possible to do so immediately without exceeding the queue's capacity, * returning {@code true} upon success and {@code false} if this queue * is full. This method is generally preferable to method {@link #add}, * which can fail to insert an element only by throwing an exception. * * @throws NullPointerException if the specified element is null */public boolean offer(E e) { checkNotNull(e); final ReentrantLock lock = this.lock; lock.lock(); try { if (count == items.length) return false; else { enqueue(e); return true; } } finally { lock.unlock(); }}put()会在队列满了的时候会阻塞生产者线程,知道有消费者线程消费后将其唤醒。
public void put(E e) throws InterruptedException { checkNotNull(e); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { while (count == items.length) notFull.await(); enqueue(e); } finally { lock.unlock(); }}private E dequeue() { // assert lock.getHoldCount() == 1; // assert items[takeIndex] != null; final Object[] items = this.items; @SuppressWarnings("unchecked") E x = (E) items[takeIndex]; items[takeIndex] = null; if (++takeIndex == items.length) takeIndex = 0; count--; if (itrs != null) itrs.elementDequeued(); notFull.signal(); // 出队后唤醒生产者线程 return x;}LinkedBlockingQueue
基于链表的阻塞队列,同ArrayListBlockingQueue类似,其内部也维持着一个数据缓冲队列(该队列由一个链表构成),当生产者往队列中放入一个数据时,队列会从生产者手中获取数据,并缓存在队列内部,而生产者立即返回;只有当队列缓冲区达到最大值缓存容量时,才会阻塞生产者队列,直到消费者从队列中消费掉一份数据,生产者线程会被唤醒,反之对于消费者这端的处理也基于同样的原理。
需要注意的是,如果构造一个LinkedBlockingQueue对象,而没有指定其容量大小,LinkedBlockingQueue会默认一个类似无限大小的容量(Integer.MAX_VALUE),这样的话,如果生产者的速度一旦大于消费者的速度,也许还没有等到队列满阻塞产生,系统内存就有可能已被消耗殆尽了。
/** * Creates a {@code LinkedBlockingQueue} with a capacity of * {@link Integer#MAX_VALUE}. */public LinkedBlockingQueue() { this(Integer.MAX_VALUE);}/** * Creates a {@code LinkedBlockingQueue} with the given (fixed) capacity. * * @param capacity the capacity of this queue * @throws IllegalArgumentException if {@code capacity} is not greater * than zero */public LinkedBlockingQueue(int capacity) { if (capacity <= 0) throw new IllegalArgumentException(); this.capacity = capacity; last = head = new Node(null);} 使用 BlockingQueue 实现生产者消费者问题
public class ProducerConsumer { private static BlockingQueue queue = new ArrayBlockingQueue<>(5); private static class Producer extends Thread { @Override public void run() { try { queue.put("product"); } catch (InterruptedException e) { e.printStackTrace(); } System.out.print("produce.."); } } private static class Consumer extends Thread { String product = queue.take(); System.out.print("consume..");}public static void main(String[] args) { for (int i = 0; i < 2; i++) { Producer producer = new Producer(); producer.start(); for (int i = 0; i < 5; i++) { Consumer consumer = new Consumer(); consumer.start(); for (int i = 0; i < 3; i++) {output:produce..produce..consume..consume..produce..consume..produce..consume..produce..consume.. 到此,相信大家对"Java中常用阻塞队列的问题是什么"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
队列
阻塞
生产
生产者
消费
数据
消费者
常用
问题
线程
容量
方法
内容
大小
数组
缓存
速度
学习
缓冲
实用
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
富士康招聘软件开发吗
义乌敏捷软件开发
花网络安全绘画儿童
永久免费云服务器不用信用卡
服务器管理添加与删除
彩虹岛2009年服务器国服
软件开发需求网站
电脑网络安全问题和解决方式
丹阳软件开发商排名
无锡定制软件开发工程师
学校加强网络安全管理制度
绵阳软件开发电话
游戏服务器开发工程师
无锡润广宣网络技术有限公司
数据库数据具有什么性
无线传感网络技术的应用案例
mysql数据库创建有默认值表
gpu服务器显卡哪个合算
力控 变量 数据库
观影笔记软件开发
网络安全使用情况记录表
python域名怎么连接服务器
关系数据库查询优化
软件开发切换城市
软件开发整套流程文件
易语言ftp服务器管理器
数据安全法 网络安全法 区别
不属于保护网络安全的
情报及网络安全保卫工作
sybase 数据库管理