LinkedBlockingQueue的put,add跟offer的区别
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的区别的更多相关文章
- LinkedBlockingQueue的put,add跟offer的区别(转)
LinkedBlockingQueue的put,add和offer的区别 最近在学习<<Java并发编程实践>>,有很多java.util.concurrent包下的新类.Li ...
- 队列Queue中add()和offer()的区别?
区别:两者都是往队列尾部插入元素,不同的时候,当超出队列界限的时候,add()方法是抛出异常让你处理,而offer()方法是直接返回false
- 队列的add与offer的区别
两个方法都表示往队列里添加元素 但是当出现异常时,add方法抛出异常 而offer则返回的是false,就是啥事也没有,也不抛异常,也没有添加成功!
- Queue接口分析:add和offer区别,remove和poll方法到底啥区别
Queue接口: public interface Queue<E> extends Collection<E> { /* * add方法,在不违背队列的容量限制的情况,往队列 ...
- JAVAAPI学习之Calendar类;Calendar类set()、add()、roll()方法区别
JAVAAPI学习之Calendar类 http://blog.csdn.net/myjlvzlp/article/details/8065775(写的很好,清晰易懂) Calendar类set(). ...
- 在 VS 类库项目中 Add Service References 和 Add Web References 的区别
原文:在 VS 类库项目中 Add Service References 和 Add Web References 的区别 出身问题: 1.在vs2005时代,Add Web Reference(添加 ...
- docker Dockerfile指令ADD和COPY的区别,添加目录方法
docker Dockerfile指令ADD和COPY的区别,添加目录方法 ADD指令的功能是将主机构建环境(上下文)目录中的文件和目录.以及一个URL标记的文件 拷贝到镜像中.其格式是: ADD 源 ...
- 从ip addr add和ifconfig的区别看linux网卡ip地址的结构
今天一个老外在邮件列表上问了一个问题,就是ip addr add和ifconfig的区别,我给他进行了解答,可能因为英语不好吧,解答的很简单,因此我还是要在这里详细说明一下.其实它们之间没有什么区别, ...
- PHP memcache add replace set的区别和其他用法收集
add replace set的区别 最近在面试时遇到一个问题 memcache 的add replace set的区别,故在此进行加强 add 是向服务器添加一个缓存的数据,当该键已存在会返回一个f ...
随机推荐
- Shell 编程基础之 For 练习
一.语法 for var in con1 con2 con3 ... do # 执行内容 done for var in {min..max} do # 执行内容 done for (( 初始值; 限 ...
- HTML5属性运用
HTML5 接触移动端,或专注于支持HTML5浏览器进行前端开发的工作者都不会陌生,这个已经普及很广,对于我专注于PC端开发的人来说,觉得陌生但又觉得很熟悉,大家都知道做PC前端开发为了兼容IE老版本 ...
- 给Nginx配置一个自签名的SSL证书
转自廖雪峰的官方网站http://www.liaoxuefeng.com/ 要保证Web浏览器到服务器的安全连接,HTTPS几乎是唯一选择.HTTPS其实就是HTTP over SSL,也就是让HTT ...
- Mishka and Divisors[CodeForces Round #365 Div.2]
http://codeforces.com/contest/703/problem/E 题意:给定一个最多个数的序列,从中选出最少个数的数字,使得他们的乘积是k的倍数,若有多种选择方式,输出选出数字和 ...
- apr 内存管理
//============================================================================ // Name : mytest.cpp ...
- Codeforces Round #210 (Div. 2) A. Levko and Table
让对角线的元素为k就行 #include <iostream> using namespace std; int main() { int n,k; cin >> n > ...
- jquery放大镜效果
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...
- ubuntu 无声音的解决
以下“失声疗法”是针对HDMI被设置为默认输出的情况: aplay -l 是用来查看音视频设备与卡号信息的: 然后 1.sudo gedit /etc/asound.conf 2.在文件中写入defa ...
- Tomcat_启动多个tomcat时,会报StandardServer.await: Invalid command '' received错误
解决方案如下:将tomcat下的server.xml文件中的端口有问题,修改规则按以下标准显示“http的端口修改为6000 to 6800之间,shutdown的端口修改为3000 to 3300之 ...
- 11877 The Coco-Cola Store
题目: 11877 The Coco-Cola Store Once upon a time, there is a special coco-cola store. If you retur ...