BlockingQueue分析

1public interface BlockingQueue<E> extends Queue<E> 2 3boolean offer(E e); 4boolean add(E e); 5 6 7When using a capacity-restricted queue, this method is generally preferable to {@link #add}, which can fail to insert an element only by throwing an exception.

当使用大小限制的queue的时候,offer要优于add。

上述方法是extends from Queue。在BlockingQueue接口中添加了方法:

1/** 2 * Inserts the specified element into this queue, waiting if necessary 3 * for space to become available. 4 * 5 * @param e the element to add 6 * @throws InterruptedException if interrupted while waiting 7 * @throws ClassCastException if the class of the specified element 8 * prevents it from being added to this queue 9 * @throws NullPointerException if the specified element is null 10 * @throws IllegalArgumentException if some property of the specified 11 * element prevents it from being added to this queue 12 */ 13 void put(E e) throws InterruptedException; 14 15 /** 16 * Inserts the specified element into this queue, waiting up to the 17 * specified wait time if necessary for space to become available. 18 * 19 * @param e the element to add 20 * @param timeout how long to wait before giving up, in units of 21 * <tt>unit</tt> 22 * @param unit a <tt>TimeUnit</tt> determining how to interpret the 23 * <tt>timeout</tt> parameter 24 * @return <tt>true</tt> if successful, or <tt>false</tt> if 25 * the specified waiting time elapses before space is available 26 * @throws InterruptedException if interrupted while waiting 27 * @throws ClassCastException if the class of the specified element 28 * prevents it from being added to this queue 29 * @throws NullPointerException if the specified element is null 30 * @throws IllegalArgumentException if some property of the specified 31 * element prevents it from being added to this queue 32 */ 33 boolean offer(E e, long timeout, TimeUnit unit) 34 throws InterruptedException;

增加了等待加入队列机制。put会一直等待,而offer则会灵活一些,等待一段时间。

-------------------取数据

queue中:

1/** 2 * Retrieves and removes the head of this queue. This method differs 3 * from {@link #poll poll} only in that it throws an exception if this 4 * queue is empty. 5 * 6 * @return the head of this queue 7 * @throws NoSuchElementException if this queue is empty 8 */ 9 E remove(); 10 11 /** 12 * Retrieves and removes the head of this queue, 13 * or returns <tt>null</tt> if this queue is empty. 14 * 15 * @return the head of this queue, or <tt>null</tt> if this queue is empty 16 */ 17 E poll(); 18 19 /** 20 * Retrieves, but does not remove, the head of this queue. This method 21 * differs from {@link #peek peek} only in that it throws an exception 22 * if this queue is empty. 23 * 24 * @return the head of this queue 25 * @throws NoSuchElementException if this queue is empty 26 */ 27 E element(); 28 29 /** 30 * Retrieves, but does not remove, the head of this queue, 31 * or returns <tt>null</tt> if this queue is empty. 32 * 33 * @return the head of this queue, or <tt>null</tt> if this queue is empty 34 */ 35 E peek();

remove,element在queue为空时会throw exception,poll,peek不会。都是取head的数据。

BlockingQueue中:

1/** 2 * Retrieves and removes the head of this queue, waiting if necessary 3 * until an element becomes available. 4 * 5 * @return the head of this queue 6 * @throws InterruptedException if interrupted while waiting 7 */ 8 E take() throws InterruptedException; 9 10 /** 11 * Retrieves and removes the head of this queue, waiting up to the 12 * specified wait time if necessary for an element to become available. 13 * 14 * @param timeout how long to wait before giving up, in units of 15 * <tt>unit</tt> 16 * @param unit a <tt>TimeUnit</tt> determining how to interpret the 17 * <tt>timeout</tt> parameter 18 * @return the head of this queue, or <tt>null</tt> if the 19 * specified waiting time elapses before an element is available 20 * @throws InterruptedException if interrupted while waiting 21 */ 22 E poll(long timeout, TimeUnit unit) 23 throws InterruptedException;

会有一个等待的过程,和加数据匹配。

int remainingCapacity();

返回理论上队列中还能存的对象个数。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java ee 部分分析

1logfilteranalysis分析filter:与Servlet相似,过滤器是一些web应用程序组件,可以绑定到一个web应用程序中。但是与其他web应用程序组件不同的是,过滤器是"链"在容器的处理过程中的。这就意味着它们会在servlet处理器之前访问一个进入的请求,并且在外发响应信息返回到客户前访问这些响应信息。这种访问使得过滤器可以检查

Jenkins流水线即代码之扩展共享库

!(https://oscimg.oschina.net/oscnet/ab8ee75c43cb1a3fd0fac241648861b03c5.gif)!(https://oscimg.oschina.net/oscnet/1a35fdf03222f188f706711d2b43eae6a14.gif)!(https://osci

HTTP面试题(二):HTTP请求报文和响应报文格式

!(https://oscimg.oschina.net/oscnet/0406894fb1274bee91fc53c84c516576.jpg)看都看了还不点个赞!(https://oscimg.oschina.net/oscnet/095d444dc9a449ee85afd19b00fdf52b.png)!(h