Example usage for java.util.concurrent ThreadPoolExecutor getCompletedTaskCount

List of usage examples for java.util.concurrent ThreadPoolExecutor getCompletedTaskCount

Introduction

In this page you can find the example usage for java.util.concurrent ThreadPoolExecutor getCompletedTaskCount.

Prototype

public long getCompletedTaskCount() 

Source Link

Document

Returns the approximate total number of tasks that have completed execution.

Usage

From source file:eu.cassandra.sim.utilities.Utils.java

public static void printExecutorSummary(ThreadPoolExecutor executor) {
    System.out.println(String.format(
            "[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s",
            executor.getPoolSize(), executor.getCorePoolSize(), executor.getActiveCount(),
            executor.getCompletedTaskCount(), executor.getTaskCount(), executor.isShutdown(),
            executor.isTerminated()));/*w  w  w.j av  a2s .c o m*/
}

From source file:Main.java

/**
 * //w w  w  .  ja  v  a  2s .com
 */
protected static void decorateProperties(ThreadPoolExecutor executor, Properties properties) {
    if (executor != null) {
        if (properties != null) {
            properties.setProperty("thread-keep-alive-time",
                    Long.toString(executor.getKeepAliveTime(TimeUnit.MILLISECONDS)));

            properties.setProperty("thread-pool-size-largest", Integer.toString(executor.getLargestPoolSize()));

            properties.setProperty("thread-pool-size-minimum", Integer.toString(executor.getCorePoolSize()));

            properties.setProperty("thread-pool-size-maximum", Integer.toString(executor.getMaximumPoolSize()));

            properties.setProperty("thread-pool-size", Integer.toString(executor.getPoolSize()));

            properties.setProperty("runnable-completed-count", Long.toString(executor.getCompletedTaskCount()));

            properties.setProperty("runnable-active-count", Integer.toString(executor.getActiveCount()));

            properties.setProperty("queue-size", Integer.toString(executor.getQueue().size()));

            properties.setProperty("queue-capacity-remaining",
                    Integer.toString(executor.getQueue().remainingCapacity()));
        }
    }
}

From source file:eu.artofcoding.beetlejuice.spring.SpringContextHelper.java

private void stopExecutors() {
    ThreadPoolTaskExecutor springTaskExecutor = applicationContext.getBean("taskExecutor",
            ThreadPoolTaskExecutor.class);
    ThreadPoolExecutor springExecutor = springTaskExecutor.getThreadPoolExecutor();
    springExecutor.shutdownNow();//  w w  w.j  a  v a  2s  . co m
    Map<String, ThreadPoolTaskExecutor> map = applicationContext.getBeansOfType(ThreadPoolTaskExecutor.class);
    ThreadPoolTaskExecutor t = null;
    for (String key : map.keySet()) {
        t = map.get(key);
        final ThreadPoolExecutor executor = t.getThreadPoolExecutor();
        executor.shutdownNow();
        logger.info(
                String.format("%s: active after shutdown: %d", executor.toString(), executor.getActiveCount()));
        logger.info(String.format("%s: completed after shutdown: %d", executor.toString(),
                executor.getCompletedTaskCount()));
    }
}

From source file:org.jmangos.commons.threadpool.CommonThreadPoolManager.java

/**
 * @see org.jmangos.commons.threadpool.ThreadPoolManager#fillPoolStats(org.jmangos.commons.threadpool.model.ThreadPoolType)
 *//*from   ww  w  .  j  a v  a 2s.  c  o  m*/
@Override
public PoolStats fillPoolStats(final ThreadPoolType poolType) {

    ThreadPoolExecutor executor = null;
    switch (poolType) {
    case INSTANT:
        executor = this.instantPool;
        break;
    case SCHEDULED:
    default:
        executor = this.scheduledPool;
        break;
    }
    final PoolStats stats = new PoolStats(poolType);
    stats.setActiveCount(executor.getActiveCount());
    stats.setCompletedTaskCount(executor.getCompletedTaskCount());
    stats.setCorePoolSize(executor.getCorePoolSize());
    stats.setLargestPoolSize(executor.getLargestPoolSize());
    stats.setMaximumPoolSize(executor.getMaximumPoolSize());
    stats.setPoolSize(executor.getPoolSize());
    stats.setQueueSize(executor.getQueue().size());
    stats.setTaskCount(executor.getTaskCount());
    return stats;
}

From source file:bigbird.benchmark.HttpBenchmark.java

public void execute() {
    params = getHttpParams(socketTimeout, useHttp1_0);

    for (RequestGenerator g : requestGenerators) {
        g.setParameters(params);/* w  ww .  ja v  a2  s .c om*/
    }

    host = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());

    ThreadPoolExecutor workerPool = new ThreadPoolExecutor(threads, threads, 5, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {

                public Thread newThread(Runnable r) {
                    return new Thread(r, "ClientPool");
                }

            });
    workerPool.prestartAllCoreThreads();

    BenchmarkWorker[] workers = new BenchmarkWorker[threads];
    for (int i = 0; i < threads; i++) {
        workers[i] = new BenchmarkWorker(params, verbosity, requestGenerators[i], host, requests, keepAlive);
        workerPool.execute(workers[i]);
    }

    while (workerPool.getCompletedTaskCount() < threads) {
        Thread.yield();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ignore) {
        }
    }

    workerPool.shutdown();
    ResultProcessor.printResults(workers, host, url.toString(), contentLength);
}

From source file:org.esigate.test.cases.PerformanceTestCase.java

/**
 * Execute la tache avec plusieurs Threads
 * /*  www . ja  v  a  2  s .c  o  m*/
 * @param request
 * @return
 * @throws Exception
 */
private long execute(HttpGetRequestRunnable request, int numberOfRequests, int threads) throws Exception {
    connectionManager = new PoolingHttpClientConnectionManager();
    httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).setMaxConnTotal(threads)
            .setMaxConnPerRoute(threads).setDefaultRequestConfig(
                    RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).build())
            .build();
    // Warm up
    request.run();

    BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
    ThreadPoolExecutor threadPool = new ThreadPoolExecutor(threads, threads, 5, TimeUnit.SECONDS, queue);

    long start = System.currentTimeMillis();
    threadPool.prestartAllCoreThreads();
    for (int i = 0; i < numberOfRequests; i++) {
        threadPool.submit(request);
    }
    threadPool.shutdown();

    // wait maximum 20 s
    threadPool.awaitTermination(200, TimeUnit.SECONDS);
    connectionManager.shutdown();

    if (request.exception != null) {
        throw new AssertionFailedError(
                "Exception for request " + request.url + " after " + request.count + " requests",
                request.exception);
    }
    if (threadPool.getCompletedTaskCount() < threadPool.getTaskCount()) {
        // All task were not executed
        String msg = request.url + " : Only " + threadPool.getCompletedTaskCount() + "/"
                + threadPool.getTaskCount() + " have been renderered " + " => Maybe a performance issue";
        threadPool.shutdownNow();
        fail(msg);
    }

    long end = System.currentTimeMillis();
    long execTime = end - start;
    LOG.debug("Executed request " + request.url + " " + numberOfRequests + " times with " + threads
            + " threads in " + execTime + "ms");
    return execTime;

}

From source file:io.anserini.index.IndexCollection.java

public void run() throws IOException, InterruptedException {
    final long start = System.nanoTime();
    LOG.info("Starting indexer...");

    int numThreads = args.threads;

    final Directory dir = FSDirectory.open(indexPath);
    final EnglishAnalyzer analyzer = args.keepStopwords ? new EnglishAnalyzer(CharArraySet.EMPTY_SET)
            : new EnglishAnalyzer();
    final IndexWriterConfig config = new IndexWriterConfig(analyzer);
    config.setSimilarity(new BM25Similarity());
    config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    config.setRAMBufferSizeMB(args.memorybufferSize);
    config.setUseCompoundFile(false);//from w w w. j a v a  2  s  .c  o  m
    config.setMergeScheduler(new ConcurrentMergeScheduler());

    final IndexWriter writer = new IndexWriter(dir, config);

    final ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(numThreads);
    final List<Path> segmentPaths = collection.getFileSegmentPaths();

    final int segmentCnt = segmentPaths.size();
    LOG.info(segmentCnt + " files found in " + collectionPath.toString());
    for (int i = 0; i < segmentCnt; i++) {
        executor.execute(new IndexerThread(writer, collection, segmentPaths.get(i)));
    }

    executor.shutdown();

    try {
        // Wait for existing tasks to terminate
        while (!executor.awaitTermination(1, TimeUnit.MINUTES)) {
            LOG.info(String.format("%.2f percent completed",
                    (double) executor.getCompletedTaskCount() / segmentCnt * 100.0d));
        }
    } catch (InterruptedException ie) {
        // (Re-)Cancel if current thread also interrupted
        executor.shutdownNow();
        // Preserve interrupt status
        Thread.currentThread().interrupt();
    }

    if (segmentCnt != executor.getCompletedTaskCount()) {
        throw new RuntimeException("totalFiles = " + segmentCnt + " is not equal to completedTaskCount =  "
                + executor.getCompletedTaskCount());
    }

    int numIndexed = writer.maxDoc();

    try {
        writer.commit();
        if (args.optimize)
            writer.forceMerge(1);
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
            // It is possible that this happens... but nothing much we can do at this point,
            // so just log the error and move on.
            LOG.error(e);
        }
    }

    LOG.info("Indexed documents: " + counters.indexedDocuments.get());
    LOG.info("Empty documents: " + counters.emptyDocuments.get());
    LOG.info("Errors: " + counters.errors.get());

    final long durationMillis = TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS);
    LOG.info("Total " + numIndexed + " documents indexed in "
            + DurationFormatUtils.formatDuration(durationMillis, "HH:mm:ss"));
}

From source file:com.espertech.esper.filter.TestIndexTreeBuilderMultithreaded.java

private void performMultithreadedTest(FilterHandleSetNode topNode, int numberOfThreads, int numberOfRunnables,
        int numberOfSecondsSleep) throws Exception {
    log.info(".performMultithreadedTest Loading thread pool work queue,numberOfRunnables=" + numberOfRunnables);

    ThreadPoolExecutor pool = new ThreadPoolExecutor(0, numberOfThreads, 99999, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>());

    for (int i = 0; i < numberOfRunnables; i++) {
        IndexTreeBuilderRunnable runnable = new IndexTreeBuilderRunnable(eventType, topNode, testFilterSpecs,
                matchedEvents, unmatchedEvents);

        pool.execute(runnable);/* w  w  w  . j  ava 2s . co  m*/
    }

    log.info(".performMultithreadedTest Starting thread pool, threads=" + numberOfThreads);
    pool.setCorePoolSize(numberOfThreads);

    // Sleep X seconds
    sleep(numberOfSecondsSleep);

    log.info(".performMultithreadedTest Completed, numberOfRunnables=" + numberOfRunnables
            + "  numberOfThreads=" + numberOfThreads + "  completed=" + pool.getCompletedTaskCount());

    pool.shutdown();
    pool.awaitTermination(1, TimeUnit.SECONDS);

    assertTrue(pool.getCompletedTaskCount() == numberOfRunnables);
}

From source file:io.anserini.index.IndexWebCollection.java

public int indexWithThreads(int numThreads) throws IOException, InterruptedException {

    LOG.info("Indexing with " + numThreads + " threads to directory '" + indexPath.toAbsolutePath() + "'...");

    final Directory dir = FSDirectory.open(indexPath);

    final IndexWriterConfig iwc = new IndexWriterConfig(new EnglishAnalyzer());

    iwc.setSimilarity(new BM25Similarity());
    iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    iwc.setRAMBufferSizeMB(512);/*from  www .j  a  va2  s  .c om*/
    iwc.setUseCompoundFile(false);
    iwc.setMergeScheduler(new ConcurrentMergeScheduler());

    final IndexWriter writer = new IndexWriter(dir, iwc);

    final ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(numThreads);
    final String suffix = Collection.GOV2.equals(collection) ? ".gz" : ".warc.gz";
    final Deque<Path> warcFiles = discoverWarcFiles(docDir, suffix);

    if (doclimit > 0 && warcFiles.size() < doclimit)
        for (int i = doclimit; i < warcFiles.size(); i++)
            warcFiles.removeFirst();

    long totalWarcFiles = warcFiles.size();
    LOG.info(totalWarcFiles + " many " + suffix + " files found under the docs path : " + docDir.toString());

    for (int i = 0; i < 2000; i++) {
        if (!warcFiles.isEmpty())
            executor.execute(new IndexerThread(writer, warcFiles.removeFirst()));
        else {
            if (!executor.isShutdown()) {
                Thread.sleep(30000);
                executor.shutdown();
            }
            break;
        }
    }

    long first = 0;
    //add some delay to let some threads spawn by scheduler
    Thread.sleep(30000);

    try {
        // Wait for existing tasks to terminate
        while (!executor.awaitTermination(1, TimeUnit.MINUTES)) {

            final long completedTaskCount = executor.getCompletedTaskCount();

            LOG.info(String.format("%.2f percentage completed",
                    (double) completedTaskCount / totalWarcFiles * 100.0d));

            if (!warcFiles.isEmpty())
                for (long i = first; i < completedTaskCount; i++) {
                    if (!warcFiles.isEmpty())
                        executor.execute(new IndexerThread(writer, warcFiles.removeFirst()));
                    else {
                        if (!executor.isShutdown())
                            executor.shutdown();
                    }
                }

            first = completedTaskCount;
            Thread.sleep(1000);
        }
    } catch (InterruptedException ie) {
        // (Re-)Cancel if current thread also interrupted
        executor.shutdownNow();
        // Preserve interrupt status
        Thread.currentThread().interrupt();
    }

    if (totalWarcFiles != executor.getCompletedTaskCount())
        throw new RuntimeException("totalWarcFiles = " + totalWarcFiles
                + " is not equal to completedTaskCount =  " + executor.getCompletedTaskCount());

    int numIndexed = writer.maxDoc();

    try {
        writer.commit();
        if (optimize)
            writer.forceMerge(1);
    } finally {
        writer.close();
    }

    return numIndexed;
}

From source file:org.apache.bookkeeper.common.util.OrderedExecutor.java

/**
 * Constructs Safe executor.//from   w w  w  .  j a v a 2 s .  co m
 *
 * @param numThreads
 *            - number of threads
 * @param baseName
 *            - base name of executor threads
 * @param threadFactory
 *            - for constructing threads
 * @param statsLogger
 *            - for reporting executor stats
 * @param traceTaskExecution
 *            - should we stat task execution
 * @param preserveMdcForTaskExecution
 *            - should we preserve MDC for task execution
 * @param warnTimeMicroSec
 *            - log long task exec warning after this interval
 * @param maxTasksInQueue
 *            - maximum items allowed in a thread queue. -1 for no limit
 */
protected OrderedExecutor(String baseName, int numThreads, ThreadFactory threadFactory, StatsLogger statsLogger,
        boolean traceTaskExecution, boolean preserveMdcForTaskExecution, long warnTimeMicroSec,
        int maxTasksInQueue, boolean enableBusyWait) {
    checkArgument(numThreads > 0);
    checkArgument(!StringUtils.isBlank(baseName));

    this.maxTasksInQueue = maxTasksInQueue;
    this.warnTimeMicroSec = warnTimeMicroSec;
    this.enableBusyWait = enableBusyWait;
    name = baseName;
    threads = new ExecutorService[numThreads];
    threadIds = new long[numThreads];
    for (int i = 0; i < numThreads; i++) {
        ThreadPoolExecutor thread = createSingleThreadExecutor(new ThreadFactoryBuilder()
                .setNameFormat(name + "-" + getClass().getSimpleName() + "-" + i + "-%d")
                .setThreadFactory(threadFactory).build());

        threads[i] = addExecutorDecorators(getBoundedExecutor(thread));

        final int idx = i;
        try {
            threads[idx].submit(() -> {
                threadIds[idx] = Thread.currentThread().getId();

                if (enableBusyWait) {
                    // Try to acquire 1 CPU core to the executor thread. If it fails we
                    // are just logging the error and continuing, falling back to
                    // non-isolated CPUs.
                    try {
                        CpuAffinity.acquireCore();
                    } catch (Throwable t) {
                        log.warn("Failed to acquire CPU core for thread {}", Thread.currentThread().getName(),
                                t.getMessage(), t);
                    }
                }
            }).get();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new RuntimeException("Couldn't start thread " + i, e);
        } catch (ExecutionException e) {
            throw new RuntimeException("Couldn't start thread " + i, e);
        }

        // Register gauges
        statsLogger.registerGauge(String.format("%s-queue-%d", name, idx), new Gauge<Number>() {
            @Override
            public Number getDefaultValue() {
                return 0;
            }

            @Override
            public Number getSample() {
                return thread.getQueue().size();
            }
        });
        statsLogger.registerGauge(String.format("%s-completed-tasks-%d", name, idx), new Gauge<Number>() {
            @Override
            public Number getDefaultValue() {
                return 0;
            }

            @Override
            public Number getSample() {
                return thread.getCompletedTaskCount();
            }
        });
        statsLogger.registerGauge(String.format("%s-total-tasks-%d", name, idx), new Gauge<Number>() {
            @Override
            public Number getDefaultValue() {
                return 0;
            }

            @Override
            public Number getSample() {
                return thread.getTaskCount();
            }
        });
    }

    // Stats
    this.taskExecutionStats = statsLogger.scope(name).getOpStatsLogger("task_execution");
    this.taskPendingStats = statsLogger.scope(name).getOpStatsLogger("task_queued");
    this.traceTaskExecution = traceTaskExecution;
    this.preserveMdcForTaskExecution = preserveMdcForTaskExecution;
}