最近在学习<<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 首先 LinkedBlockingQueue 是一个 “可选且有界” 的阻塞队列实现,你可以根据需要指定队列的大小。 接下来,我将创建一个 LinkedBlockingQueue ,它最多可以包含100个元素: BlockingQueue...
linkedblockingqueue测试程序
并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法
并发容器之ArrayBlockingQueue和LinkedBlockingQueue实现原理详解
单向链表源码 博文链接:https://uule.iteye.com/blog/1561753
LinkedBlockingQueuejava.pdf
NULL 博文链接:https://xiongjiajia.iteye.com/blog/2325943
JDK容器学习之Queue:LinkedBlockingQueue
主要介绍了详细分析Java并发集合LinkedBlockingQueue的用法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了java中LinkedBlockingQueue与ArrayBlockingQueue的异同,需要的朋友可以参考下
NULL 博文链接:https://kanpiaoxue.iteye.com/blog/2101309
主要重点是在不牺牲可读性的前提下实现简单性和高性能。 实际上,我试图提供有关代码的良好文档以及一些用法示例。 提供的队列 ArrayBlockingQueue :由切片支持的有界阻塞队列 LinkedBlockingQueue :由容器/列表...
container Collection 标记: 顶级接口 List 标记: interface ArrayList 标记: class ...boolean add(E e) ...boolean add(E e) ...LinkedBlockingQueue 链表结构实现,无界队列(默认上限Integer.MAX_VALUE)
Java线程池的几种实现方法和区别介绍 使用:LinkedBlockingQueue实现线程池讲解
java-basicJava基础的一些测试:例如 java8 的功能;多线程集合IOand so onJava中的集合?1.核心接口:Iterator(迭代器接口)Iterable(可迭代接口) -> ...1.1Queue队列接口: 入队:add() offer() 出队:remove() pee
ArrayBlockingQueue和PriorityBlockingQueue使用较少,一般使用LinkedBlockingQueue和Synchronous。线程池的排队策略与BlockingQueue有关。 threadFactory:线程工厂,主要用来创建线程:默认值 ...
LinkedBlockingQueue以及redis队列写入mysql实例
java.util.concurrent总体概览图。 收取资源分3分。需要的同学可以下载一下。 java.util.concurrent主要包括5个部分executor,colletions,locks,atomic...该图详细的列举了并发包下面的结构,包含所有接口和具体实现类。