Example usage for java.util.concurrent BlockingQueue size

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

Introduction

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

Prototype

int size();

Source Link

Document

Returns the number of elements in this collection.

Usage

From source file:DelayedJob.java

public static void main(String[] args) throws InterruptedException {
    BlockingQueue<DelayedJob> queue = new DelayQueue<>();
    Instant now = Instant.now();

    queue.put(new DelayedJob("A", now.plusSeconds(9)));
    queue.put(new DelayedJob("B", now.plusSeconds(3)));
    queue.put(new DelayedJob("C", now.plusSeconds(6)));
    queue.put(new DelayedJob("D", now.plusSeconds(1)));

    while (queue.size() > 0) {
        System.out.println("started...");
        DelayedJob job = queue.take();//from w  ww. ja  v  a2  s  .  com
        System.out.println("Job: " + job);
    }
    System.out.println("Finished.");
}

From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerWithRabbitMQ.java

private static String count(List<BlockingQueue<?>> queues) {
    int n = 0;//from  w  ww  .  j a va 2s. c  om
    for (BlockingQueue<?> queue : queues) {
        n += queue.size();
    }
    return "Total queue size: " + n;
}

From source file:org.loklak.android.wok.Harvester.java

/**
 * if the given list of timelines contain at least the wanted minimum size of
 * messages, they are flushed from the queue and combined into a new timeline
 * //from  w w w.j  a  v a 2  s .c o m
 * @param dumptl
 * @param order
 * @param minsize
 * @return
 */
public static Timeline takeTimelineMin(final BlockingQueue<Timeline> dumptl, final Timeline.Order order,
        final int minsize) {
    int c = 0;
    for (Timeline tl : dumptl)
        c += tl.size();
    if (c < minsize)
        return new Timeline(order);

    // now flush the timeline queue completely
    Timeline tl = new Timeline(order);
    try {
        while (dumptl.size() > 0) {
            Timeline tl0 = dumptl.take();
            if (tl0 == null)
                return tl;
            tl.putAll(tl0);
        }
        return tl;
    } catch (InterruptedException e) {
        return tl;
    }
}

From source file:org.apache.usergrid.tools.ImportAdmins.java

private static void waitForQueueAndMeasure(final BlockingQueue workQueue, final AtomicInteger emptyCounter,
        final Map<Stoppable, Thread> threadMap, final String identifier) throws InterruptedException {
    double rateAverageSum = 0;
    int iterations = 0;

    while (emptyCounter.get() < threadMap.size()) {
        iterations += 1;//from  w  w w  .  j  av  a  2 s . c  om

        int sizeLast = workQueue.size();
        long lastTime = System.currentTimeMillis();
        logger.info("Queue {} is not empty, remaining size={}, waiting...", identifier, sizeLast);
        Thread.sleep(10000);

        long timeNow = System.currentTimeMillis();
        int sizeNow = workQueue.size();

        int processed = sizeLast - sizeNow;

        long timeDelta = timeNow - lastTime;

        double rateLast = (double) processed / (timeDelta / 1000);
        rateAverageSum += rateLast;

        long timeRemaining = (long) (sizeLast / (rateAverageSum / iterations));

        logger.info(
                "++PROGRESS ({}): sizeLast={} nowSize={} processed={} rateLast={}/s rateAvg={}/s timeRemaining={}s",
                new Object[] { identifier, sizeLast, sizeNow, processed, rateLast,
                        (rateAverageSum / iterations), timeRemaining });
    }

    for (Stoppable worker : threadMap.keySet()) {
        worker.setDone(true);
    }
}

From source file:org.apache.hadoop.hbase.ipc.BalancedQueueRpcExecutor.java

@Override
public int getQueueLength() {
    int length = 0;
    for (final BlockingQueue<CallRunner> queue : queues) {
        length += queue.size();
    }/*from   ww w.ja v  a2 s . c o m*/
    return length;
}

From source file:net.sf.jacclog.service.importer.internal.queue.LogFileQueueImporterObserver.java

@Override
public void empty(final BlockingQueue<LogFile> queue) {
    LOG.info("Log file queue is is empty. (size: " + queue.size() + ")");
}

From source file:org.apache.camel.impl.DefaultServicePool.java

public synchronized int size() {
    int size = 0;
    for (BlockingQueue<Service> entry : pool.values()) {
        size += entry.size();
    }/* w ww . j  ava2s .co  m*/
    return size;
}

From source file:org.apache.hadoop.hbase.ipc.BalancedQueueRpcExecutor.java

@Override
public boolean dispatch(final CallRunner callTask) throws InterruptedException {
    int queueIndex = balancer.getNextQueue();
    BlockingQueue<CallRunner> queue = queues.get(queueIndex);
    // that means we can overflow by at most <num reader> size (5), that's ok
    if (queue.size() >= currentQueueLimit) {
        return false;
    }/*from ww  w.j  a v  a  2s.c o m*/
    return queue.offer(callTask);
}

From source file:net.sf.jacclog.service.importer.internal.queue.LogEntryQueuePersisterObserver.java

@Override
public void empty(final BlockingQueue<ReadonlyLogEntry> queue) {
    LOG.debug("Log entry queue is empty. (size: " + queue.size() + ")");

    // cleaning task
    executor.execute(new LogEntryPersisterTask(service, queue));
}

From source file:com.fluidops.iwb.api.CommunicationServiceImpl.java

protected static void handlePendingRequestsInternal() {
    BlockingQueue<UpdateRequest> requests = CommunicationServiceImpl.requests.instance();

    Long start = System.currentTimeMillis();

    handleRequestWarning(requests.size());

    // NOTE: we implement a while on top of the boolean
    // variable (rather than the requests variable itself)
    // in order to be able to push the synchronized block inside;
    // this allows for interleaving of pushing and polling
    // and thus improved eCM core performance, as it imposes minimal
    // blocking time to the process filling the requests array
    boolean requestsEmpty = false;

    Set<URI> updatedURIs = new HashSet<URI>();
    Set<URI> deletedURIs = new HashSet<URI>();
    Set<URI> contextsWithRemoveOp = new HashSet<URI>();
    Set<URI> contextsWithAddOp = new HashSet<URI>();

    int ctr = 0;//from   w w  w.ja  va2 s  .  c om
    ReadWriteDataManager dm = null;
    try {
        while (!requestsEmpty) {
            UpdateRequest request = null;
            if (requests.isEmpty())
                requestsEmpty = true; // abort
            else
                request = requests.poll();

            // we process the request outside of the synchronized
            // block, in order to release the lock as early as possible
            if (request != null) {
                try {
                    // we only open the data manager if required, and only once
                    if (dm == null)
                        dm = ReadWriteDataManagerImpl.openDataManager(Global.repository);

                    request.handleRequest(dm, updatedURIs, deletedURIs, contextsWithRemoveOp,
                            contextsWithAddOp);
                    ctr++;
                    if ((ctr % 1000) == 0)
                        logger.info(
                                "Synching requests into INT DB - count=" + ctr + " queue=" + requests.size());
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                }
            }
        }

        if (ctr > 0) // something has been changed
        {
            String cleanupMsg = dm.cleanupMetaGarbage(contextsWithRemoveOp);

            KeywordIndexAPI.updateUrisInIndex(updatedURIs);
            KeywordIndexAPI.updateUrisInIndex(deletedURIs);

            logger.debug("Synchronized " + ctr + " objects to INT database in "
                    + (System.currentTimeMillis() - start) + "ms (" + cleanupMsg + ")");
        } // otherwise: no action has been performed, nothing to do
    } finally {
        ReadWriteDataManagerImpl.closeQuietly(dm);
    }
}