阻塞队列的take、offer、put、add的一些比较
LinkedBlockingQueue的put,add和offer的区别
最近在学习<<Java并发编程实践>>,有很多java.util.concurrent包下的新类。LinkedBlockingQueue就是其中之一,顾名思义这是一个阻塞的线程安全的队列,底层应该采用链表实现。
看其API的时候发现,添加元素的方法竟然有三个:add,put,offer。
且这三个元素都是向队列尾部添加元素的意思。于是我产生了兴趣,要仔细探究一下他们之间的差别。
1.首先看一下add方法:
- Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.
- This implementation returns true if offer succeeds, else throws an IllegalStateException.
LinkedBlockingQueue构造的时候若没有指定大小,则默认大小为Integer.MAX_VALUE,当然也可以在构造函数的参数中指定大小。LinkedBlockingQueue不接受null。
add方法在添加元素的时候,若超出了度列的长度会直接抛出异常:
- public static void main(String args[]){
- try {
- LinkedBlockingQueue<String> queue=new LinkedBlockingQueue(2);
- queue.add("hello");
- queue.add("world");
- queue.add("yes");
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- //运行结果:
- java.lang.IllegalStateException: Queue full
- at java.util.AbstractQueue.add(Unknown Source)
- at com.wjy.test.GrandPather.main(GrandPather.java:12)
2.再来看一下put方法:
- Inserts the specified element at the tail of this queue, waiting if necessary for space to become available.
对于put方法,若向队尾添加元素的时候发现队列已经满了会发生阻塞一直等待空间,以加入元素。
- public static void main(String args[]){
- try {
- LinkedBlockingQueue<String> queue=new LinkedBlockingQueue(2);
- queue.put("hello");
- queue.put("world");
- queue.put("yes");
- System.out.println("yes");
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- //运行结果:
- //在queue.put("yes")处发生阻塞
- //下面的“yes”无法输出
3.最后看一下offer方法:
- 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 true upon success and false if this queue is full. When using a capacity-restricted queue, this method is generally preferable to method add, which can fail to insert an element only by throwing an exception.
offer方法在添加元素时,如果发现队列已满无法添加的话,会直接返回false。
- public static void main(String args[]){
- try {
- LinkedBlockingQueue<String> queue=new LinkedBlockingQueue(2);
- boolean bol1=queue.offer("hello");
- boolean bol2=queue.offer("world");
- boolean bol3=queue.offer("yes");
- System.out.println(queue.toString());
- System.out.println(bol1);
- System.out.println(bol2);
- System.out.println(bol3);
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- //运行结果:
- [hello, world]
- true
- true
- false
好了,竟然说了这么多了,就把从队列中取元素的方法也顺便一说。
从队列中取出并移除头元素的方法有:poll,remove,take。
总结:
take和put是阻塞的获取和存储元素的方法,
poll和offer是不阻塞的获取元素和存储元素的方法,并且poll和offer可以指定超时时间。
add和remove存取元素,队列满时add抛异常,队列空时remove抛异常
poll: 若队列为空,返回null。
remove:若队列为空,抛出NoSuchElementException异常。
take:若队列为空,发生阻塞,等待有元素。
阻塞队列的take、offer、put、add的一些比较的更多相关文章
- java并发:阻塞队列
第一节 阻塞队列 1.1 初识阻塞队列 队列以一种先进先出的方式管理数据,阻塞队列(BlockingQueue)是一个支持两个附加操作的队列,这两个附加的操作是:在队列为空时,获取元素的线程会等待队列 ...
- 学习笔记之Java队列Queue中offer/add函数,poll/remove函数,peek/element函数的区别
队列是一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作. LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用. Java中Que ...
- 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题
调用同步锁的wait().notify().notifyAll()进行线程通信 看这个经典的存取款问题,要求两个线程存款,两个线程取款,账户里有余额的时候只能取款,没余额的时候只能存款,存取款金额相同 ...
- Java中的阻塞队列
1. 什么是阻塞队列? 阻塞队列(BlockingQueue)是一个支持两个附加操作的队列.这两个附加的操作是:在队列为空时,获取元素的线程会等待队列变为非空.当队列满时,存储元素的线程会等待队列可用 ...
- java并发编程学习: 阻塞队列 使用 及 实现原理
队列(Queue)与栈(Stack)是数据结构中的二种常用结构,队列的特点是先进先出(First In First Out),而Stack是先进后出(First In Last Out),说得通俗点: ...
- java并发编程:阻塞队列
一.几种主要的阻塞队列 自从Java 1.5之后,在java.util.concurrent包下提供了若干个阻塞队列,主要有以下几个: ArrayBlockingQueue:基于数组实现的一个阻塞队列 ...
- JAVA可阻塞队列-ArrayBlockingQueue
在前面的的文章,写了一个带有缓冲区的队列,是用JAVA的Lock下的Condition实现的,但是JAVA类中提供了这项功能,就是ArrayBlockingQueue, ArrayBlockingQu ...
- 阻塞队列--LinkedBlockingQueue
什么叫线程安全?线程安全就是每次运行结果和单线程运行的结果是一样的,而且其他的变量的值也和预期的是一样的. 线程安全就是说多线程访问同一代码,不会产生不确定的结果. 并行和并发区别1.并行是指两者同时 ...
- 聊聊并发(七)——Java中的阻塞队列
3. 阻塞队列的实现原理 聊聊并发(七)--Java中的阻塞队列 作者 方腾飞 发布于 2013年12月18日 | ArchSummit全球架构师峰会(北京站)2016年12月02-03日举办,了解更 ...
- Java并发编程:阻塞队列(转载)
Java并发编程:阻塞队列 在前面几篇文章中,我们讨论了同步容器(Hashtable.Vector),也讨论了并发容器(ConcurrentHashMap.CopyOnWriteArrayList), ...
随机推荐
- MeteoInfoLab脚本示例:闪电位置图
这个脚本示例读取文本格式的闪电数据,读出每条闪电记录的经纬度和强度,在地图上绘制出每个闪电的位置,并用符号和颜色区分强度正负.数据格式如下:0 2009-06-06 00:01:16.6195722 ...
- swoft生成控制器
[root@localhost swoft]# swoftcli gen:controller -h [ERROR] Command 'controller' is not exist in grou ...
- HTML <big> 标签
HTML <big> 标签 什么是<big> 标签? <big> 标签呈现大号字体效果. 使用 <big> 标签可以很容易地放大字体.这简直不能再简单了 ...
- QJsonObject 遍历
遍历QjsonObject方式 方式一 QJsonObject::const_iterator it = l_obj.constBegin(); QJsonObject::const_iterator ...
- python 虚拟环境安装
windows虚拟环境的搭建 安装 # 建议使用pip3安装到python3环境下 pip3 install virtualenv pip3 install virtualenvwrapper-win ...
- OpenCV计算机视觉学习(7)——图像金字塔(高斯金字塔,拉普拉斯金字塔)
如果需要处理的原图及代码,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice 本节 ...
- 最新版Python 3.8.6 版本发布
Python 3.8.6 发布了,它是 Python 3.8 的第六个维护版本. 3.8 系列的维护版本将每两个月定期更新一次,3.8.7 计划于 2020 年 11 月中旬发布. 随着维护版本的发布 ...
- 逆向so文件调试工具ida基础知识点
1.界面介绍 https://www.freebuf.com/column/157939.html 2.IDA常用快捷键 切换文本视图与图表视图 空格键 返回上一个操作地址 ESC 搜索地址和符号 G ...
- 线程池ScheduledThreadPool
定时线程池 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle ...
- Spring Shiro配置第三方SSO客户端登录
经过实践的Shiro配置,利用 sSOInterceptor 进行sso登录拦截 配置 @Configuration public class ShiroConfiguration extends B ...