API这么一段换,英语不咋地,有点看不懂,求翻译、求指教
However之后看不懂鸟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.
However之后看不懂鸟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 接口的实现必须是线程安全的!
他的父类是AbstractQueue,里面有addAll这个方法,大侠们帮忙鉴定一下,我感觉根本不是同步的:
public boolean addAll(Collection<? extends E> c) {
if (c == null)
throw new NullPointerException();
if (c == this)
throw new IllegalArgumentException();
boolean modified = false;
for (E e : c)
if (add(e))
modified = true;
return modified;
}
里面又调用了add:
public boolean add(E e) {
if (offer(e))
return true;
else
throw new IllegalStateException("Queue full");
}
又调用了offer这offer没找着实现悲剧鸟
BlockingQueue实现是线程安全的。所有的队列方法的行为都是使用内部锁或其它并发控制的行式实现原子性,然而,大集合的操作addAll, containsAll, retainAll and removeAll没有必要实现原子性除非在实现中指定了。所以可能发生这样的事儿,例如,addAll(c)失败(抛出一个异常)之后可能添加进去的只是c中一部分元素。
意思大概就是说:
BlockingQueue本身的方法是线程安全的
但是addAll, containsAll, retainAll and removeAll不是线程安全的。