使用BlockingQueue怎么实现阻塞队列
发表于:2025-12-01 作者:千家信息网编辑
千家信息网最后更新 2025年12月01日,这篇文章将为大家详细讲解有关使用BlockingQueue怎么实现阻塞队列,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。package com.shi
千家信息网最后更新 2025年12月01日使用BlockingQueue怎么实现阻塞队列
这篇文章将为大家详细讲解有关使用BlockingQueue怎么实现阻塞队列,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
package com.shi.queue;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;import java.util.concurrent.TimeUnit;/** * 阻塞队列 * @author shiye * */public class TestBlockQueue { public static void main(String[] args) throws InterruptedException { //定义一个长度为3的阻塞队列 BlockingQueue blockingQueue = new ArrayBlockingQueue<>(3); System.out.println("----------------抛出异常的 情况----------------------");// blockingQueue.add("aa");// blockingQueue.add("bb");// blockingQueue.add("cc"); //blockingQueue.add("dd");//如果队列满了 Exception java.lang.IllegalStateException: Queue full // System.out.println(blockingQueue.element());//检查队列头的信息 : aa // blockingQueue.remove();// blockingQueue.remove();// blockingQueue.remove(); //blockingQueue.remove();//如果队列为空 Exception java.util.NoSuchElementException //System.out.println(blockingQueue.element());//如果队列为空 Exception java.util.NoSuchElementException System.out.println("----------------返回true/false----------------------");// System.out.println(blockingQueue.offer("11"));//插入队列 true// System.out.println(blockingQueue.offer("22"));//插入队列 true// System.out.println(blockingQueue.offer("33"));//插入队列 true// System.out.println(blockingQueue.offer("44"));//插入队列 false// // System.out.println(blockingQueue.peek());//检查队列头元素 11// // System.out.println(blockingQueue.poll());//输出队列 11// System.out.println(blockingQueue.poll());//输出队列 22// System.out.println(blockingQueue.poll());//输出队列 33// System.out.println(blockingQueue.poll());//输出队列 null System.out.println("----------------队列阻塞等待----------------------");// blockingQueue.put("zhangsan");// blockingQueue.put("lisi");// blockingQueue.put("wangwu");// //blockingQueue.put("shiye");//线程一直等待无法关闭// // blockingQueue.take();// blockingQueue.take();// blockingQueue.take(); //blockingQueue.take();//线程一直等待 无法响应 System.out.println("----------------队列等待一定时间之后就退出----------------------"); System.out.println(blockingQueue.offer("aa", 2, TimeUnit.SECONDS));//true System.out.println(blockingQueue.offer("aa", 2, TimeUnit.SECONDS));//true System.out.println(blockingQueue.offer("aa", 2, TimeUnit.SECONDS));//true System.out.println(blockingQueue.offer("aa", 2, TimeUnit.SECONDS));//false 等待2s钟之后就退出 }} SynchronousQueue
package com.shi.queue;import java.util.concurrent.BlockingQueue;import java.util.concurrent.SynchronousQueue;/** * 不存储数据的队列,即生产一个消费一个的队列 * @author shiye * *结果: AA 存放1 ... BB get 1 AA 存放2 ... BB get 2 AA 存放3 ... BB get 3 */public class TestSynchroniousQueue { public static void main(String[] args) { BlockingQueue blockingQueue = new SynchronousQueue<>(); new Thread(()-> { try { System.out.println(Thread.currentThread().getName()+ "\t 存放1 ..." ); blockingQueue.put("1"); System.out.println(Thread.currentThread().getName()+ "\t 存放2 ..." ); blockingQueue.put("2"); System.out.println(Thread.currentThread().getName()+ "\t 存放3 ..." ); blockingQueue.put("3"); } catch (InterruptedException e) { e.printStackTrace(); } },"AA").start(); new Thread(()-> { try { Thread.sleep(5000);//睡眠5秒 System.out.println(Thread.currentThread().getName() + "\t get " + blockingQueue.take()); Thread.sleep(5000);//睡眠5秒 System.out.println(Thread.currentThread().getName() + "\t get " + blockingQueue.take()); Thread.sleep(5000);//睡眠5秒 System.out.println(Thread.currentThread().getName() + "\t get " + blockingQueue.take()); } catch (InterruptedException e) { e.printStackTrace(); } },"BB").start(); }} 综合案例(使用阻塞队列实现生产者消费者问题)
package com.shi.queue;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;import java.util.concurrent.CountDownLatch;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicInteger;/** * 通过阻塞队列的方式 实现 生产者 消费者 问题 * @author shiye * 使用到的技术: * countDownLatch:闭锁 * volatile 自旋锁 * AtomicInteger 原子整型 * BlockingQueue 阻塞队列 * */public class TestProducterAndConsumterByQueue { public static void main(String[] args) throws InterruptedException { //闭锁 final CountDownLatch countDownLatch = new CountDownLatch(11); Check check = new Check(new ArrayBlockingQueue<>(3)); //创建线程生产 (启动10个线程去生产) for (int i = 0; i < 10; i++) { new Thread(()->{ System.out.println(Thread.currentThread().getName() + "\t 生产者启动..."); try { check.productor("aaa"); } catch (InterruptedException e) { e.printStackTrace(); } countDownLatch.countDown();//线程数量减一 },"AA-"+i).start(); } //创建1 个线程消费 new Thread(()->{ System.out.println(Thread.currentThread().getName() + "\t 消费者启动..."); try { check.consumter(); } catch (InterruptedException e) { e.printStackTrace(); } countDownLatch.countDown();//线程数量减一 },"BB").start(); Thread.sleep(5000);//等待5秒 停止 check.stop(); countDownLatch.await();//等待上面的线程全部执行完毕,才检查产品数量 System.out.println("5s之后线程停止,总共生产了:"+ check.getTotle() +"件产品"); }}//店员class Check{ private volatile boolean FLAG = true;//标志位 private AtomicInteger atomicInteger = new AtomicInteger();//统计总数的变量 private BlockingQueue关于使用BlockingQueue怎么实现阻塞队列就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
队列
线程
生产
阻塞
消费
消费者
生产者
数据
睡眠
输出
数量
检查
产品
内容
总数
文章
更多
知识
篇文章
问题
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
s3和s5服务器的区别
手机软件开发技能
江苏常见软件开发维修电话
中国食品安全 数据库
中学网络安全宣传周信息
快手挂榜软件开发
千万条网络安全合规
消防部队网络安全责任书
服务器安全组设置视频
软件开发自考真题
计算机网络技术三级序列号
网络安全青年文明号
计算机网络技术该看什么书
网络安全与网络攻防发言
广州弘泽网络技术
深信服网络安全是什么
腾讯qq数据库
软件开发属什么学校
提升网络安全风险发现能力
服务器安全狗4.2下载安装
dayz特朗普服务器安全区
数据库服务安卓下载
著名十大网络安全事件
民法典中网络安全的内容
计算机网络安全试题调研历史
软件开发哪个岗位待遇好
科钥网络安全
安徽宿州网络技术有限公司
安哥拉的软件开发
服务器放一段时间后开机不亮屏