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

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

时间: 2024-08-27 20:16:01

LinkedBlockingQueue的put,add跟offer的区别(转)的相关文章

LinkedBlockingQueue的put,add跟offer的区别

LinkedBlockingQueue的put,add和offer的区别 最近在学习<<Java并发编程实践>>,有很多java.util.concurrent包下的新类.LinkedBlockingQueue就是其中之一,顾名思义这是一个阻塞的线程安全的队列,底层应该采用链表实现. 看其API的时候发现,添加元素的方法竟然有三个:add,put,offer. 且这三个元素都是向队列尾部添加元素的意思.于是我产生了兴趣,要仔细探究一下他们之间的差别. 1.首先看一下add方法: I

LinkedList中add和offer的区别

offer属于 offer in interface Deque<E>,add 属于 add in interface Collection<E>. 当队列为空时候,使用add方法会报错,而offer方法会返回false. 作为List使用时,一般采用add / get方法来 压入/获取对象. 作为Queue使用时,才会采用 offer/poll/take等方法作为链表对象时,offer等方法相对来说没有什么意义这些方法是用于支持队列应用的.  

java 学习之List 的 add 与set方法区别

/** * 在List集合中众多方法中,add(int index,Object obj)方法与set(int index,Object e)方法不易区分 * ,通过下面实例,可以看出两个方法中的区别 */ package gao; import java.util.Iterator; import java.util.LinkedList; import java.util.List; public class CollectionDemo { public static void main(

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

JAVAAPI学习之Calendar类 http://blog.csdn.net/myjlvzlp/article/details/8065775(写的很好,清晰易懂) Calendar类set().add().roll()方法区别 http://www.360doc.com/content/15/0616/16/25883431_478549940.shtml http://blog.csdn.net/csdnbenbenchong/article/details/7010908

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

add replace set的区别 最近在面试时遇到一个问题 memcache 的add replace set的区别,故在此进行加强 add 是向服务器添加一个缓存的数据,当该键已存在会返回一个false,否则返回一个true replace 是在服务器内一个替换一个缓存的数据,当该键不存在时会返回一个false,否则返回true set 则是add和replace的集合体,如果该键存在就替换,不存在就设置,返回的是true increment decrement delete加法减法删除运

Add Service References 和 Add Web References的区别

Add Service References 和 Add Web References的区别 在项目过程中,需要添加Web服务引用,发现两种用法.搜索了一下相关信息,才知道: VS2005里提供的Add Web Reference(添加Web服务引用)的功能主要是添加Web Service引用. VS2008保留了Add Web Reference(添加Web服务引用)也是为了版本向前兼容.目前很多项目还是基于.NET Framework 2.0. VS2008在升级以后为了对.NET Fram

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

原文:在 VS 类库项目中 Add Service References 和 Add Web References 的区别 出身问题: 1.在vs2005时代,Add Web Reference(添加Web服务引用)的功能主要是添加Web Service引用.基于.NET Framework 2.0. 2.自VS2008以后,为了对.NET Framework 3.0 或 3.5版本上WCF Service Library的支持.增加了Add Service Reference(添加服务引用)功

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

今天一个老外在邮件列表上问了一个问题,就是ip addr add和ifconfig的区别,我给他进行了解答,可能因为英语不好吧,解答的很简单,因此我还是要在这里详细说明一下.其实它们之间没有什么区别,只 是表述方式不同罢了.如果你非常理解网络协议的原理以及网络的分层架构那么我想你就不会有这个问题,实际上,每一个网卡设备都有一个mac地址,但是却可 以有多个网络层地址,比如IP地址,然而这个事实无法很好地像用户提供操作接口,所以就引出了ip别名(IP aliases)和辅助ip(secondary

Add和AddRange的区别

Add和AddRange区别 Add和AddRange Add:将指定的对象添加到……中 AddRange:向……末尾,添加数组 - 在群体操作时,使用AddRange取代Add 用AddRange可以让我们要加入的东西一次性加入,而不要每次都加一次,这样显然可以加快速度.几乎所有的windows control都支持Add和AddRange两种方法. Add: For i = 0 To param.Length - 1 comm.Parameters.Add(param(i)) Next i