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. 

解决方案 »

  1.   


    不会吧?BlockingQueue 接口的实现必须是线程安全的!
      

  2.   

    说白了就是 BlockingQueue 的实现都是线程安全的,特别是一些用于队列的方法,是通过内部的锁来保证并发控制的。
      

  3.   

    刚翻了一一下源码,看的是BlockingQueue的这个实现:LinkedBlockingQueue。
    他的父类是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没找着实现悲剧鸟
      

  4.   

    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.   

    necessarily 还有必定的意思。也就是说,我们在做集合操作的时候,那么这些方法不一定表现出原子性。
      

  6.   

    5楼翻译的由点意思, 不过在用BlockingQueue的时候基本用不到那几个不安全的方法, BlockingQueue最大的好处就是可以控制在Queue中元素的个数,如果队列中已经达到了峰值,这是如果再加新元素,那么BlockingQueue会自动的把第一个加入的元素去掉,从而可以加入新元素到最后一位。在使用的过程中也能保证最先到Queue的元素先被取得。 总体来说BlockingQueue还是非常好用的
      

  7.   

    也不知BlockingQueue的各种实现类是不是都这样子