阻塞队列BlockingQueue

本文介绍了Java中的阻塞队列BlockingQueue,包括其关系图和四种操作形式:添加和删除。 BlockingQueue结合了集合的增删改查功能,并提供线程安全的数据同步操作。
摘要由CSDN通过智能技术生成

1. 关系图

加粗样式
由上图可以看出,BlockingQueue有集合的增删改查的特性。

2. 阻塞队列的四种形式

# BlockingQueue接口上的注释
BlockingQueue methods come in four forms, with different ways of handling operations that cannot be satisfied immediately, but may be satisfied at some point in the future: one throws an exception, the second returns a special value (either null or false, depending on the operation), the third blocks the current thread indefinitely until the operation can succeed, and the fourth blocks for only a given maximum time limit before giving up. These methods are summarized in the following table:
Summary of BlockingQueue methods
A BlockingQueue does not accept null elements.  Implementations throw NullPointerException on attempts to add, put or offer a null.  A null is used as a sentinel value to indicate failure of poll operations.
BlockingQueue implementations are designed to be used primarily for producer-consumer queues, but additionally support the Collection interface.  So, for example, it is possible to remove an arbitrary element from a queue using remove(x).  However, such operations are in general not performed very efficiently, and are intended for only occasional use, such as when a queued message is cancelled. 
BlockingQueue implementations are thread-safe.  All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control.  However, the bulk Collection operations addAll, containsAll, retainAll and removeAll are not necessarily performed atomically unless specified otherwise in an implementation.  So it is possible, for example, for addAll(c) to fail (throwing an exception) after adding only some of the elements in c. 
A BlockingQueue does not intrinsically support any kind of "close" or "shutdown" operation to indicate that no more items will be added.  The needs and usage of such features tend to be implementation-dependent.  For example, a common tactic is for producers to insert special end-of-stream or poison objects, that are interpreted accordingly when taken by consumers. 
Usage example, based on a typical producer-consumer scenario
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值