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。

poll: 若队列为空,返回null。

remove:若队列为空,抛出NoSuchElementException异常。

take:若队列为空,发生阻塞,等待有元素。

LinkedBlockingQueue的put,add跟offer的区别的更多相关文章

  1. LinkedBlockingQueue的put,add跟offer的区别(转)

    LinkedBlockingQueue的put,add和offer的区别 最近在学习<<Java并发编程实践>>,有很多java.util.concurrent包下的新类.Li ...

  2. 队列Queue中add()和offer()的区别?

    区别:两者都是往队列尾部插入元素,不同的时候,当超出队列界限的时候,add()方法是抛出异常让你处理,而offer()方法是直接返回false

  3. 队列的add与offer的区别

    两个方法都表示往队列里添加元素 但是当出现异常时,add方法抛出异常 而offer则返回的是false,就是啥事也没有,也不抛异常,也没有添加成功!

  4. Queue接口分析:add和offer区别,remove和poll方法到底啥区别

    Queue接口: public interface Queue<E> extends Collection<E> { /* * add方法,在不违背队列的容量限制的情况,往队列 ...

  5. JAVAAPI学习之Calendar类;Calendar类set()、add()、roll()方法区别

    JAVAAPI学习之Calendar类 http://blog.csdn.net/myjlvzlp/article/details/8065775(写的很好,清晰易懂) Calendar类set(). ...

  6. 在 VS 类库项目中 Add Service References 和 Add Web References 的区别

    原文:在 VS 类库项目中 Add Service References 和 Add Web References 的区别 出身问题: 1.在vs2005时代,Add Web Reference(添加 ...

  7. docker Dockerfile指令ADD和COPY的区别,添加目录方法

    docker Dockerfile指令ADD和COPY的区别,添加目录方法 ADD指令的功能是将主机构建环境(上下文)目录中的文件和目录.以及一个URL标记的文件 拷贝到镜像中.其格式是: ADD 源 ...

  8. 从ip addr add和ifconfig的区别看linux网卡ip地址的结构

    今天一个老外在邮件列表上问了一个问题,就是ip addr add和ifconfig的区别,我给他进行了解答,可能因为英语不好吧,解答的很简单,因此我还是要在这里详细说明一下.其实它们之间没有什么区别, ...

  9. PHP memcache add replace set的区别和其他用法收集

    add replace set的区别 最近在面试时遇到一个问题 memcache 的add replace set的区别,故在此进行加强 add 是向服务器添加一个缓存的数据,当该键已存在会返回一个f ...

随机推荐

  1. 跳转页面&回到上一页

    1.php <?php header('Location:2.php'); //echo不能在其前面 echo '1'; 2.js <?php echo '2'; echo '<me ...

  2. <构建之法>第十一章、十二章有感

    十一章:软件设计与实现 工作时要懂得平衡进度和质量.我一直有一个困扰:像我们团队这次做 男神女神配 社区交友网,我负责主页的设计及内容模块,有个队友负责网站的注册和登录模块,有个队友负责搜索模块,有个 ...

  3. 测试Centos硬盘读写速度

    yum install hdparm 参数:  -a<快取分区>   设定读取文件时,预先存入块区的分区数,若不加上<快取分区>选项,则显示目前的设定.  -A<0或1& ...

  4. Zepto Code Rush 2014 B - Om Nom and Spiders

    注意题目给的是一个nxm的park,设元素为aij,元素aij 有4种可能U(上移),D(下移),L(左移),R(右移) 假设第i行第j列元素aij(注意元素的索引是从0开始的) 当aij为D时,此时 ...

  5. ACM 16进制的简单运算

    16进制的简单运算 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 现在给你一个16进制的加减法的表达式,要求用8进制输出表达式的结果.   输入 第一行输入一个正整 ...

  6. Codeforces Round #228 (Div. 2) B. Fox and Cross

    #include <iostream> #include <string> #include <vector> #include <algorithm> ...

  7. ACM: 强化训练-Inversion Sequence-线段树 or STL·vector

    Inversion Sequence Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%lld & %llu D ...

  8. 关于scrollbar-face-color只支持ie的解决办法!

    关于scrollbar-face-color只支持ie的解决方法!!今天突然有人问我滚动条css自定义的方法,我发现用scrollbar-base-color这种方法只有ie支持,查了半天资料总结如下 ...

  9. android注意事项

    今天做安卓设计,正在学习布局.在过程中遇到了几个小问题,感觉非常有必要记录分享出来. 1.string字符串不要出现"that's" ,要使用“that is”要不然会报错. 2. ...

  10. JsonP的简单demo

    服务器端代码 public ActionResult GetNewUploadCourseIds() { "; var result = new Result<NewUpload> ...