Example usage for java.util.concurrent BlockingQueue remainingCapacity

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

Introduction

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

Prototype

int remainingCapacity();

Source Link

Document

Returns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking, or Integer.MAX_VALUE if there is no intrinsic limit.

Usage

From source file:com.seajas.search.contender.service.monitoring.MonitoringService.java

/**
 * Count the remaining queue capacity within the enricher executor pool.
 * //from  w  w w .ja v a2 s  .co m
 * @return Long
 * @throws JMException
 */
public Long countEnricherExecutorPoolRemainingQueueCapacity() {
    BlockingQueue<Runnable> queue = enricherExecutorPool.getThreadPoolExecutor().getQueue();

    return (long) queue.remainingCapacity();
}

From source file:com.slytechs.jnetstream.livecapture.AbstractLiveCapture.java

/**
 * Dispatches a packet that is ready to clients. A concurrent blocking queue
 * is used to pass packets between this server thread and clients. Each client
 * maintains its own queue through which the packet is exchanged. If
 * particular clients queue is full, the packet is dropped and the appropriate
 * counter incremented to indicate a drop, on this particular client's queue.
 *///from ww w  .  java 2s  .co  m
public void dispatch(final LivePacket packet) {
    if (client == null) {
        return; // Nothing to do, ignore any packets received
    }

    if (logger.isTraceEnabled()) {
        logger.trace("Received packet");
    }

    final BlockingQueue<LivePacket> queue = client.getPacketQueue();

    if (queue.remainingCapacity() == 0) {
        client.incrementDropCounter(1);

    } else {
        queue.offer(packet);
    }

    synchronized (DISPATCHED) {
        this.dispatched++;
    }
}

From source file:gobblin.couchbase.writer.CouchbaseWriterTest.java

private void drainQueue(BlockingQueue<Pair<AbstractDocument, Future>> queue, int threshold, long sleepTime,
        TimeUnit sleepUnit, List<Pair<AbstractDocument, Future>> failedFutures) {
    while (queue.remainingCapacity() < threshold) {
        if (sleepTime > 0) {
            Pair<AbstractDocument, Future> topElement = queue.peek();
            if (topElement != null) {
                try {
                    topElement.getSecond().get(sleepTime, sleepUnit);
                } catch (Exception te) {
                    failedFutures.add(topElement);
                }/*www  .ja  v a 2 s .  c om*/
                queue.poll();
            }
        }
    }
}

From source file:de.tudarmstadt.lt.seg.app.Segmenter.java

private void run_parallel() throws Exception {

    InputStream in = System.in;
    if (!"-".equals(_filename_in))
        in = new FileInputStream(_filename_in);
    Stream<String> liter = new BufferedReader(new InputStreamReader(in, Charset.defaultCharset())).lines();

    ThreadLocal<ISentenceSplitter> sentenceSplitter = ThreadLocal.withInitial(() -> {
        try {/*from www  .j av  a2  s.  c o  m*/
            return newSentenceSplitter();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    });
    ThreadLocal<ITokenizer> tokenizer = ThreadLocal.withInitial(() -> {
        try {
            return newTokenizer();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    });

    final PrintWriter[] w = new PrintWriter[_parallelism];
    // init writers
    for (int i = 0; i < _parallelism; i++) {
        OutputStream out = System.out;
        if (!"-".equals(_filename_out)) {
            out = new FileOutputStream(String.format("%s_%d", _filename_out, i));
        }
        w[i] = new PrintWriter(new OutputStreamWriter(out, Charset.defaultCharset()));
    }

    BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(_parallelism * 2, true);
    ExecutorService es = new ThreadPoolExecutor(_parallelism, _parallelism, 0L, TimeUnit.MILLISECONDS, queue);

    AtomicLong lc = new AtomicLong(0);
    liter.forEach((line) -> {
        // don't try to submit new threads, wait until the thread queue has some capacity again
        while (queue.remainingCapacity() == 0)
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                /**/}
        es.submit(() -> {
            final long docid = lc.incrementAndGet();
            if (docid % 1000 == 0)
                System.err.format("Processing line %d ('%s')%n", docid, _filename_in);
            final int w_i = (int) (docid % _parallelism);
            split_and_tokenize(new StringReader(line.trim()), String.format("%s:%d", _filename_in, docid),
                    sentenceSplitter.get(), tokenizer.get(), _level_filter, _level_normalize, _merge_types,
                    _merge_tokens, _separator_sentence, _separator_token, _separator_desc, w[w_i]);

        });
    });
    es.shutdown();
    es.awaitTermination(Integer.MAX_VALUE, TimeUnit.DAYS);

    // TODO: the stream parallelism version does not work because it submits too many threads at once
    //      AtomicLong lc = new AtomicLong(0);
    //      ForkJoinPool forkJoinPool = new ForkJoinPool(_parallelism);
    //      forkJoinPool.submit(() -> 
    //         liter.parallel().forEach((line) -> {
    //            final long docid = lc.incrementAndGet();
    //            if(docid % 1000 == 0)
    //               System.err.format("Processing line %d ('%s')%n", docid, _filename_in);
    //   
    //            String l = line.replace("\\t", "\t").replace("\\n", "\n");
    //            split_and_tokenize(
    //                  new StringReader(l),
    //                  String.format("%s:%d", _filename_in, docid),
    //                  sentenceSplitter.get(), 
    //                  tokenizer.get(), 
    //                  _level_filter,
    //                  _level_normalize,
    //                  _merge_types,
    //                  _merge_tokens,
    //                  _separator_sentence,
    //                  _separator_token,
    //                  _separator_desc,
    //                  w);
    //      })).get();

}

From source file:net.centro.rtb.monitoringcenter.MetricCollectorImpl.java

@Override
public <T> BlockingQueue<T> instrumentBlockingQueue(final BlockingQueue<T> blockingQueue, String topLevelName,
        String... additionalNames) {
    validateInstrumentationInputParams(blockingQueue, "blockingQueue", topLevelName);

    String queueNamespace = MetricNamingUtil.join(topLevelName, additionalNames);

    registerGauge(new Gauge<Integer>() {
        @Override/*from w ww .j  ava 2 s  . co m*/
        public Integer getValue() {
            return blockingQueue.size();
        }
    }, MetricNamingUtil.join(queueNamespace, "size"));

    registerGauge(new Gauge<Integer>() {
        @Override
        public Integer getValue() {
            return blockingQueue.remainingCapacity();
        }
    }, MetricNamingUtil.join(queueNamespace, "remainingCapacity"));

    return blockingQueue;
}

From source file:org.apache.hadoop.ipc.FairCallQueue.java

/**
 * Returns maximum remaining capacity. This does not reflect how much you can
 * ideally fit in this FairCallQueue, as that would depend on the scheduler's
 * decisions.// w w w .j av a  2s .c  om
 */
@Override
public int remainingCapacity() {
    int sum = 0;
    for (BlockingQueue<E> q : this.queues) {
        sum += q.remainingCapacity();
    }
    return sum;
}

From source file:org.muehleisen.hannes.taxiapp.TaxiRoute.java

@Override
public void run() {
    log.info(this.getClass().getSimpleName() + " starting...");

    BlockingQueue<Runnable> taskQueue = new LinkedBlockingDeque<Runnable>(100);
    ExecutorService ex = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
            Runtime.getRuntime().availableProcessors(), Integer.MAX_VALUE, TimeUnit.DAYS, taskQueue,
            new ThreadPoolExecutor.DiscardPolicy());
    // create rainbow table for driver lookup
    log.info("Creating driver license rainbow table.");
    RouteLogEntry.initLrt();/*from   w w  w.j a v a2  s .co m*/

    // bring up routing service
    log.info("Bringing up OTP Graph Service from '" + graph + "'.");
    GraphServiceImpl graphService = new GraphServiceImpl();
    graphService.setPath(graph);
    graphService.startup();
    ps = new RetryingPathServiceImpl(graphService, new EarliestArrivalSPTService());

    // read taxi files
    log.info("Reading taxi files from '" + taxilog + "'.");
    Collection<File> files = FileUtils.listFiles(new File(taxilog), new SuffixFileFilter(".csv.zip"),
            TrueFileFilter.INSTANCE);
    for (File f : files) {
        log.info("Reading '" + f + "'.");
        try {
            ZipInputStream z = new ZipInputStream(new FileInputStream(f));
            z.getNextEntry(); // ZIP files have many entries. In this case,
                              // only one
            BufferedReader r = new BufferedReader(new InputStreamReader(z));
            r.readLine(); // header
            String line = null;
            while ((line = r.readLine()) != null) {
                RouteLogEntry rle = new RouteLogEntry(line);
                if (!rle.hasGeo()) {
                    continue;
                }
                while (taskQueue.remainingCapacity() < 1) {
                    Thread.sleep(100);
                }
                ex.submit(new RouteTask(rle));
            }
            r.close();
            z.close();
        } catch (Exception e) {
            log.error("Failed to read taxi file from '" + taxilog + "'.", e);
        }
    }
    ex.shutdown();
    try {
        ex.awaitTermination(Integer.MAX_VALUE, TimeUnit.DAYS);
    } catch (InterruptedException e) {
        // ...
    }
    log.info(deliveries);
}