Example usage for java.util.concurrent BlockingQueue drainTo

List of usage examples for java.util.concurrent BlockingQueue drainTo

Introduction

In this page you can find the example usage for java.util.concurrent BlockingQueue drainTo.

Prototype

int drainTo(Collection<? super E> c, int maxElements);

Source Link

Document

Removes at most the given number of available elements from this queue and adds them to the given collection.

Usage

From source file:Main.java

/**
 * Drains the queue as {@link BlockingQueue#drainTo(Collection, int)}, but if the requested
 * {@code numElements} elements are not available, it will wait for them up to the specified
 * timeout.//  w w  w  . j  a  va2 s. c  o m
 *
 * Taken from Google Guava 18.0 Queues
 *
 * @param q the blocking queue to be drained
 * @param buffer where to add the transferred elements
 * @param numElements the number of elements to be waited for
 * @param timeout how long to wait before giving up, in units of {@code unit}
 * @param unit a {@code TimeUnit} determining how to interpret the timeout parameter
 * @param <E> the type of the queue
 * @return the number of elements transferred
 * @throws InterruptedException if interrupted while waiting
 */
public static <E> int drain(BlockingQueue<E> q, Collection<? super E> buffer, int numElements, long timeout,
        TimeUnit unit) throws InterruptedException {
    buffer = Objects.requireNonNull(buffer);
    /*
     * This code performs one System.nanoTime() more than necessary, and in return, the time to
     * execute Queue#drainTo is not added *on top* of waiting for the timeout (which could make
     * the timeout arbitrarily inaccurate, given a queue that is slow to drain).
     */
    long deadline = System.nanoTime() + unit.toNanos(timeout);
    int added = 0;
    while (added < numElements) {
        // we could rely solely on #poll, but #drainTo might be more efficient when there are multiple
        // elements already available (e.g. LinkedBlockingQueue#drainTo locks only once)
        added += q.drainTo(buffer, numElements - added);
        if (added < numElements) { // not enough elements immediately available; will have to poll
            E e = q.poll(deadline - System.nanoTime(), TimeUnit.NANOSECONDS);
            if (e == null) {
                break; // we already waited enough, and there are no more elements in sight
            }
            buffer.add(e);
            added++;
        }
    }
    return added;
}

From source file:org.apache.hadoop.hbase.regionserver.MemStoreChunkPool.java

/**
 * Add the chunks to the pool, when the pool achieves the max size, it will
 * skip the remaining chunks//from   w w w  .  j  a va  2s  . c  o m
 * @param chunks
 */
void putbackChunks(BlockingQueue<Chunk> chunks) {
    int maxNumToPutback = this.maxCount - reclaimedChunks.size();
    if (maxNumToPutback <= 0) {
        return;
    }
    chunks.drainTo(reclaimedChunks, maxNumToPutback);
}

From source file:org.apache.synapse.transport.amqp.AMQPTransportUtils.java

/**
 * Move elements between buffers. No need of additional synchronization locks,
 * BlockingQueue#drainTo is thread safe, but not atomic, which is not a problem.
 * See {@link BlockingQueue#drainTo(java.util.Collection, int)}
 *
 * @param src       source buffer//from   w ww.  j  a  v  a  2  s . c  o  m
 * @param dest      destination buffer
 * @param blockSize blockSize of message bulk that need to move
 * @throws AMQPTransportException in case of drains fails
 */

public static void moveElements(BlockingQueue<AMQPTransportMessage> src, List<AMQPTransportMessage> dest,
        final int blockSize) throws AMQPTransportException {
    try {
        src.drainTo(dest, blockSize);
    } catch (Exception e) {
        throw new AMQPTransportException(e.getMessage(), e);
    }
}

From source file:org.wso2.carbon.cloud.gateway.common.CGUtils.java

/**
 * Move elements between buffers. No need of additional synchronization locks,
 * BlockingQueue#drainTo is thread safe, but not atomic, which is not a problem.
 * See {@link BlockingQueue#drainTo(java.util.Collection, int)}
 *
 * @param src       source buffer/* www  .  ja v  a 2  s.  co m*/
 * @param dest      destination buffer
 * @param blockSize blockSize of message bulk that need to move
 * @throws AxisFault in case of drains fails
 */
public static void moveElements(BlockingQueue<Message> src, List<Message> dest, final int blockSize)
        throws AxisFault {
    try {
        src.drainTo(dest, blockSize);
    } catch (Exception e) {
        throw new AxisFault(e.getMessage(), e);
    }
}