LinkedBlockingQueue的put,add跟offer的区别

LinkedBlockingQueue的put,add和offer的区别
最近看Flume的源码,MemoryChannel队列就是用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

从队列中取元素的方法也有3种。
从队列中取出并移除头元素的方法有:poll,remove,take。

  • poll: 若队列为空,返回null。
  • remove:若队列为空,抛出NoSuchElementException异常。
  • take:若队列为空,发生阻塞,等待有元素。
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转载自:Java集合框架实例 1- 介绍 集合是程序和语言的基本思想。应用程序通常都会应用到集合,例如雇员的信息,...
    01_小小鱼_01阅读 428评论 0 1
  • The Evolution of the Producer-Consumer Problem in Java Ja...
    Binzo阅读 538评论 0 0
  • 昨归家。进门见母亲方备祭品烧纸等物欲祭外婆,当地俗称“烧十月一”。我言自己一人去烧祭外婆即可,母执意去之,说等明年...
    沃原阅读 146评论 0 0
  • 初中时喜欢坐我右手边的男生,个子小小的,脑袋圆圆的,数学棒棒的。 他上课睡觉,我默默盯梢。发卷子我先帮他看...
    nice念念不忘阅读 296评论 0 0
  • 我们为什么在一段又一段的时间节点就会想着改变呢?因为我们都向往美好的事情发生,向往一个更好的自己,所以即使总是有三...
    懒虫的忧虑生活阅读 361评论 0 0