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:若队列为空,发生阻塞,等待有元素。

http://www.soso.io/article/66762.html

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. 扩展WebBrowser控件,使其支持拖放文件

    public partial class UserControl1 : WebBrowser { private const int WmDropfiles = 0x233; [DllImport(& ...

  2. 黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block 代理对象(Proxy Object) ...

  3. 第三章 AOP 编程选择

    Spring为我们开发者提供了多种AOP的编程方式.我们该如何选择呢? 如果项目采用的是JDK5.0以上版本,我们可以选择@AspectJ的方式.这是第一选择. http://blog.csdn.ne ...

  4. Gulp实现服务器

    Gulp实现web服务器 Gulp实现web服务器 阅读目录 一:gulp实现web服务器配置: 二:添加实时刷新(livereload)支持 回到顶部 一:gulp实现web服务器配置: 对于前端开 ...

  5. 朴素UNIX它-Linux CFS注视

    该系列产品,被称为纯UNIX,但它也包含各种类别UNIX该系统的细节,自从完成我多年的学习笔记本系列文章,分析了各种UNIX,类UNIX思想和情感的实现. 这篇文章是比较短.只是分析Linux CFS ...

  6. Android 调试native的crash和anr

    1. 于trace找到相应的库.例如 liba.so和相应的地址信息 2. 采用addr2line 查看 addr2line 住址 -e liba.so -f 要么 arm-eabi-addr2lin ...

  7. Vue.js学习与理解

    Vue.js(读音 /vjuː/, 类似于 view)是一个构建数据驱动的 web 界面的库.Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件. Vue.js 自身不 ...

  8. Visual Studio 有哪些好用的插件?

    推荐一批绝大部分都是免费的能较好增强写代码舒适度的. .NET Demon -- (非免费)安装后可以连续编译, 如果不买License的话过期后也还有代码改动后自动保存的上好功能, 安装它之后再也不 ...

  9. eclipse-jee 配置tomcat7,解决404错误

    在eclipse的Servers窗口新建一个tomcat7,配置tomcat的安装路径,然后启动tomcat,访问http://localhost:8080/,但是报404错误,恼火!没有找到要访问的 ...

  10. 程序员---C语言细节7(增加两个整数溢出检测)

    主要内容:增加两个整数溢出检测 #include <stdio.h> #include <limits.h> int main(int argc, char *argv[]) ...