public int drainTo(Collection<? super E> collection, int maxElements)
    {
        if (collection == null)
        {
            throw new IllegalArgumentException("The 'collection' parameter may not be null.");
        }

        if (collection == this)
        {
            throw new IllegalArgumentException("The 'collection' parameter may not be this object.");
        }

        // final Queue<E> items = this.buffer;
        ReentrantLock lock = this.lock;
        lock.lock();

        try
        {
            int n = 0;

            for (int max = ((maxElements >= count) || (maxElements < 0)) ? count : maxElements; n < max; n++)
            {
                // Take items from the queue, do unblock the producers, but don't send not full signals yet.
                collection.add(extract(true, false).getElement());
            }

            if (n > 0)
            {
                // count -= n;
                notFull.signalAll();
            }

            return n;
        }
        finally
        {
            lock.unlock();
        }
    }
public int drainTo ( Collection < ? super E > collection , int maxElements ) { if ( collection == null ) { throw new IllegalArgumentException ( "The 'collection' parameter may not be null." ) ; } if ( collection == this ) { throw new IllegalArgumentException ( "The 'collection' parameter may not be this object." ) ; } ReentrantLock lock = this . lock ; lock . lock ( ) ; try { int n = 0 ; for ( int max = ( ( maxElements >= count ) || ( maxElements < 0 ) ) ? count : maxElements ; n < max ; n ++ ) { collection . add ( extract ( true , false ) . getElement ( ) ) ; } if ( n > 0 ) { notFull . signalAll ( ) ; } return n ; } finally { lock . unlock ( ) ; } }
Removes at most the given number of available elements from this queue and adds them into the given collection. A
failure encountered while attempting to <tt>add</tt> elements to collection <tt>c</tt> may result in elements
being in neither, either or both collections when the associated exception is thrown. Attempts to drain a queue
to itself result in <tt>IllegalArgumentException</tt>. Further, the behavior of this operation is undefined if
the specified collection is modified while the operation is in progress.

@param  collection  The collection to transfer elements into.
@param  maxElements The maximum number of elements to transfer. If this is -1 then that is interpreted as meaning
all elements.

@return The number of elements transferred.

@throws IllegalArgumentException If collection is this queue or null.