LinkedBlockingQueue实现定长队列自动出队与读取队列 原创 2021-04-23 16:10:02.0 阅读(1718)次 我要实现一个存储请求状态的队列缓存,需要用到LinkedBlockingQueue,实现定长队列自动出队,另外还要实现不出队的情况下读取队列数据。 代码如下: ```java LinkedBlockingQueue queue = new LinkedBlockingQueue(3); queue.offer(1); queue.offer(2); queue.offer(3); boolean offer = queue.offer(4); if(offer == false){ System.out.println("自动进出"); queue.poll(); queue.offer(4); } System.out.println(offer); System.out.println("完成"+queue.size()); Object[] objects = queue.toArray(); for (Object o : objects){ System.out.println("ll:"+o); } ``` 其中: queue.poll(); queue.offer(4); 先出再进是用来自动出队保持定长数据。并发下要上锁。 其中: Object[] objects = queue.toArray(); 就是用来实现队列在不出队的情况下读取队列里的数据。 java 上一篇:解决java.lang.OutOfMemoryError: unable to create new native thread 下一篇:java List间计算并集、差集、交集以及去重
相关文章 QLExpress if判断以及多个if else如何写(2603) QLExpress在脚本中执行Java代码(2529) java表达式运算性能比较:Jep与QLExpress(2480) 切分List集合为多个List集合(7509) bootstrap.yml配置报错:Could not resolve placeholder 'xx' in value (7078) ThreadLocal正确使用姿势,避免再掉坑(1214) java使用snakeyaml库读取和操作yml文件(2496) java通过域名获取域名的IP地址(1520) Jep实现Java表达式计算(2057) Java实现Https发送get和post请求(2738) 推荐文章 使用spring4实现websocket连接(1) Parameter index out of range (1 > number of parameters, which is 0(7) spring cloud+feign+mybatis中使用seata0.9实现分布式事务(7) spring cloud gateway报错Only one connection receive subscriber allowed(82) spring cloud中Feign调用诡异报错MethodNotAllowed: status 405 reading(116) elasticsearch7.1保存时报错: Validation Failed: 1: type is missing;(7) 聊聊数据保存到MySQL后数据乱码的问题(1) jquery对象与dom对象互转(1) linux使用epel源yum安装iftop、nload、nginx等(2) linux下nginx安装其他模块(1) 热门文章 java stream去重的几种方式(40819) the dependencies of some of the beans in the application context form a cycle(16324) 解决mybatis打印查询结果集造成太多日志的问题(9839) java enum枚举转list和Map(9722) java stream List转Map与List转List与Map转List以及List转Map(8468) ServletRequest转HttpServletRequest设置header之后取不到header的问题(8434) java中BufferedImage转成 base64字符串(8098) 切分List集合为多个List集合(7385) maven修改jar包版本不生效解决办法(6990) bootstrap.yml配置报错:Could not resolve placeholder 'xx' in value (6949) 标签列表 java java java java java java java基础 微服务 异常处理 mysql clickhouse clickhouse clickhouse clickhouse clickhouse spring cloud spring boot linux elasticsearch feign jdbc spring js docker postgresql solr seata nginx maven gateway hsqldb 数据库 架构 大数据分析 分布式事务 redis canal dubbo hadoop 消息队列 win10 websocket springmvc git html select2 mybatis jenkins rocketmq quartz activemq 数据库集群 ajax bat 电脑 笔记 eclipse 设计模式 阿里云 github freemarker jvm jquery javamail redission redission对象 hystrix http hibernate springmail svn ubuntu ueditor xheditor zookeeper 分布式 小程序 开发工具 gitlab