LinkedBlockingQueue的put,add跟offer的区别

1.首先看一下add方法:

  1. 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.   
  2. 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 {
            LinkedBlockingDeque<String> queue = new LinkedBlockingDeque<String>(2);
            queue.add("hello");
            queue.add("world");
            queue.add("yes");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
运行结果:
java.lang.IllegalStateException: Deque full
	at java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:289)
	at java.util.concurrent.LinkedBlockingDeque.add(LinkedBlockingDeque.java:582)
	at com.alipay.demo.trade.service.impl.hb.BlockingQueueMethodTest.main(BlockingQueueMethodTest.java:14)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

 

 

 2.再来看一下put方法:

  1. Inserts the specified element at the tail of this queue, waiting if necessary for space to become available.  

 

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方法:

 

  1. 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.  

 

 

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值