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.
------解决方案-------------------- 说白了就是 BlockingQueue 的实现都是线程安全的,特别是一些用于队列的方法,是通过内部的锁来保证并发控制的。
------解决方案-------------------- 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. BlockingQueue实现是线程安全的。所有的队列方法的行为都是使用内部锁或其它并发控制的行式实现原子性,然而,大集合的操作addAll, containsAll, retainAll and removeAll没有必要实现原子性除非在实现中指定了。所以可能发生这样的事儿,例如,addAll(c)失败(抛出一个异常)之后可能添加进去的只是c中一部分元素。 意思大概就是说: BlockingQueue本身的方法是线程安全的 但是addAll, containsAll, retainAll and removeAll不是线程安全的。
------解决方案-------------------- 5楼翻译的由点意思, 不过在用BlockingQueue的时候基本用不到那几个不安全的方法, BlockingQueue最大的好处就是可以控制在Queue中元素的个数,如果队列中已经达到了峰值,这是如果再加新元素,那么BlockingQueue会自动的把第一个加入的元素去掉,从而可以加入新元素到最后一位。在使用的过程中也能保证最先到Queue的元素先被取得。 总体来说BlockingQueue还是非常好用的