Example usage for java.util.concurrent ExecutorService invokeAll

List of usage examples for java.util.concurrent ExecutorService invokeAll

Introduction

In this page you can find the example usage for java.util.concurrent ExecutorService invokeAll.

Prototype

<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException;

Source Link

Document

Executes the given tasks, returning a list of Futures holding their status and results when all complete.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    ExecutorService service = Executors.newFixedThreadPool(5);
    List<Future<java.lang.String>> futureList = service
            .invokeAll(Arrays.asList(new Task1<String>(), new Task2<String>()));

    System.out.println(futureList.get(1).get());
    System.out.println(futureList.get(0).get());
}

From source file:Main.java

public static void main(String[] args) throws InterruptedException {
    ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);
    List<Particle> allTheParticles = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        allTheParticles.add(new Particle(i, allTheParticles));
    }//from  www. j a v  a 2s  .  c om
    while (true) {
        executor.invokeAll(allTheParticles);
        executor.invokeAll(allTheParticles);
    }
}

From source file:com.linkedin.pinot.perf.FilterOperatorBenchmark.java

public static void main(String[] args) throws Exception {
    String rootDir = args[0];//  w w w .  ja v  a  2  s.  com
    File[] segmentDirs = new File(rootDir).listFiles();
    String query = args[1];
    AtomicInteger totalDocsMatched = new AtomicInteger(0);
    Pql2Compiler pql2Compiler = new Pql2Compiler();
    BrokerRequest brokerRequest = pql2Compiler.compileToBrokerRequest(query);
    List<Callable<Void>> segmentProcessors = new ArrayList<>();
    long[] timesSpent = new long[segmentDirs.length];
    for (int i = 0; i < segmentDirs.length; i++) {
        File indexSegmentDir = segmentDirs[i];
        System.out.println("Loading " + indexSegmentDir.getName());
        Configuration tableDataManagerConfig = new PropertiesConfiguration();
        List<String> invertedColumns = new ArrayList<>();
        FilenameFilter filter = new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".bitmap.inv");
            }
        };
        String[] indexFiles = indexSegmentDir.list(filter);
        for (String indexFileName : indexFiles) {
            invertedColumns.add(indexFileName.replace(".bitmap.inv", ""));
        }
        tableDataManagerConfig.setProperty(IndexLoadingConfigMetadata.KEY_OF_LOADING_INVERTED_INDEX,
                invertedColumns);
        IndexLoadingConfigMetadata indexLoadingConfigMetadata = new IndexLoadingConfigMetadata(
                tableDataManagerConfig);
        IndexSegmentImpl indexSegmentImpl = (IndexSegmentImpl) Loaders.IndexSegment.load(indexSegmentDir,
                ReadMode.heap, indexLoadingConfigMetadata);
        segmentProcessors
                .add(new SegmentProcessor(i, indexSegmentImpl, brokerRequest, totalDocsMatched, timesSpent));
    }
    ExecutorService executorService = Executors.newCachedThreadPool();
    for (int run = 0; run < 5; run++) {
        System.out.println("START RUN:" + run);
        totalDocsMatched.set(0);
        long start = System.currentTimeMillis();
        List<Future<Void>> futures = executorService.invokeAll(segmentProcessors);
        for (int i = 0; i < futures.size(); i++) {
            futures.get(i).get();
        }
        long end = System.currentTimeMillis();
        System.out.println("Total docs matched:" + totalDocsMatched + " took:" + (end - start));
        System.out.println("Times spent:" + Arrays.toString(timesSpent));
        System.out.println("END RUN:" + run);
    }
    System.exit(0);
}

From source file:com.mapr.synth.Synth.java

public static void main(String[] args)
        throws IOException, CmdLineException, InterruptedException, ExecutionException {
    final Options opts = new Options();
    CmdLineParser parser = new CmdLineParser(opts);
    try {/*from  ww w.j  a  v a 2s  . c  o m*/
        parser.parseArgument(args);
    } catch (CmdLineException e) {
        System.err.println("Usage: " + "[ -count <number>G|M|K ] " + "-schema schema-file "
                + "[-quote DOUBLE_QUOTE|BACK_SLASH|OPTIMISTIC] " + "[-format JSON|TSV|CSV|XML ] "
                + "[-threads n] " + "[-output output-directory-name] ");
        throw e;
    }
    Preconditions.checkArgument(opts.threads > 0 && opts.threads <= 2000,
            "Must have at least one thread and no more than 2000");

    if (opts.threads > 1) {
        Preconditions.checkArgument(!"-".equals(opts.output),
                "If more than on thread is used, you have to use -output to set the output directory");
    }

    File outputDir = new File(opts.output);
    if (!"-".equals(opts.output)) {
        if (!outputDir.exists()) {
            Preconditions.checkState(outputDir.mkdirs(),
                    String.format("Couldn't create output directory %s", opts.output));
        }
        Preconditions.checkArgument(outputDir.exists() && outputDir.isDirectory(),
                String.format("Couldn't create directory %s", opts.output));
    }

    if (opts.schema == null) {
        throw new IllegalArgumentException("Must specify schema file using [-schema filename] option");
    }
    final SchemaSampler sampler = new SchemaSampler(opts.schema);
    final AtomicLong rowCount = new AtomicLong();

    final List<ReportingWorker> tasks = Lists.newArrayList();
    int limit = (opts.count + opts.threads - 1) / opts.threads;
    int remaining = opts.count;
    for (int i = 0; i < opts.threads; i++) {

        final int count = Math.min(limit, remaining);
        remaining -= count;

        tasks.add(new ReportingWorker(opts, sampler, rowCount, count, i));
    }

    final double t0 = System.nanoTime() * 1e-9;
    ExecutorService pool = Executors.newFixedThreadPool(opts.threads);
    ScheduledExecutorService blinker = Executors.newScheduledThreadPool(1);
    final AtomicBoolean finalRun = new AtomicBoolean(false);

    final PrintStream sideLog = new PrintStream(new FileOutputStream("side-log"));
    Runnable blink = new Runnable() {
        public double oldT;
        private long oldN;

        @Override
        public void run() {
            double t = System.nanoTime() * 1e-9;
            long n = rowCount.get();
            System.err.printf("%s\t%d\t%.1f\t%d\t%.1f\t%.3f\n", finalRun.get() ? "F" : "R", opts.threads,
                    t - t0, n, n / (t - t0), (n - oldN) / (t - oldT));
            for (ReportingWorker task : tasks) {
                ReportingWorker.ThreadReport r = task.report();
                sideLog.printf("\t%d\t%.2f\t%.2f\t%.2f\t%.1f\t%.1f\n", r.fileNumber, r.threadTime, r.userTime,
                        r.wallTime, r.rows / r.threadTime, r.rows / r.wallTime);
            }
            oldN = n;
            oldT = t;
        }
    };
    if (!"-".equals(opts.output)) {
        blinker.scheduleAtFixedRate(blink, 0, 10, TimeUnit.SECONDS);
    }
    List<Future<Integer>> results = pool.invokeAll(tasks);

    int total = 0;
    for (Future<Integer> result : results) {
        total += result.get();
    }
    Preconditions.checkState(total == opts.count, String
            .format("Expected to generate %d lines of output, but actually generated %d", opts.count, total));
    pool.shutdownNow();
    blinker.shutdownNow();
    finalRun.set(true);
    sideLog.close();
    blink.run();
}

From source file:Main.java

public static void invokeBulkActions(Collection<Callable<Object>> tasks, int numFixedThreads) {
    ExecutorService executor = Executors.newFixedThreadPool(numFixedThreads);
    try {//from  w w w.  j a  v  a 2s .  co  m
        executor.invokeAll(tasks);
    } catch (InterruptedException iex) {
    } finally {
        executor.shutdown();
    }
}

From source file:Main.java

public static <T> List<T> executeParallel(final List<Callable<T>> callables, final int maxThreadCount)
        throws InterruptedException, ExecutionException {
    final int threadCount = callables.size() > 0 && callables.size() < maxThreadCount ? callables.size()
            : maxThreadCount;//from   w  w  w . j a  v  a2 s .  c om
    ExecutorService executor = newFixedThreadPool(threadCount);
    List<T> results = new ArrayList<>();
    try {
        for (Future<T> future : executor.invokeAll(callables)) {
            results.add(future.get());
        }
    } finally {
        executor.shutdown();
    }
    return results;
}

From source file:com.epam.catgenome.util.HistogramUtils.java

/**
 * Executes histogram creation, done by callable tasks
 * @param executorService an ExecutorService to execute histogram building tasks
 * @param histogramTasks callable tasks, that create histogram blocks
 * @return a List of {@link Wig} blocks, representing histogram blocks
 * @throws HistogramWritingException if something goes wrong during execution
 *//*ww  w . jav a  2s .c  o m*/
public static List<Wig> executeHistogramCreation(final ExecutorService executorService,
        final List<Callable<List<Wig>>> histogramTasks) throws HistogramWritingException {
    final List<Wig> histogram = new ArrayList<>();
    try {
        executorService.invokeAll(histogramTasks).forEach(future -> addBlockToHistogram(future, histogram));
    } catch (InterruptedException | AssertionError e) {
        throw new HistogramWritingException(e);
    }
    return histogram;
}

From source file:api.startup.PDFIndexer.java

/**
 * Indexes all the documents in the CSV file
 * @param writer - index writer/*w  ww . j  a  va2 s  .  c  o m*/
 * @param indexConfiguration - the configuration for all the indexable documents
 * @throws IOException
 */
static void indexDocs(final IndexWriter writer, String indexConfiguration) throws IOException {
    Reader in = new FileReader(indexConfiguration);
    CSVParser parser = CSVFormat.RFC4180.withHeader().parse(in);
    List<Callable<Object>> tasks = new ArrayList<>();
    int threadPoolSize = Runtime.getRuntime().availableProcessors();
    log.info("Indexing with " + threadPoolSize + " processors");
    ExecutorService pool = Executors.newFixedThreadPool(threadPoolSize);
    for (CSVRecord record : parser) {
        DocumentMetadata meta = new DocumentMetadata(record);
        tasks.add(() -> {
            indexDoc(writer, meta);
            return null;
        });
    }

    try {
        pool.invokeAll(tasks);
    } catch (InterruptedException e) {
        log.error("Indexing was interrupted " + e.getMessage());
    }

}

From source file:org.apache.sysml.runtime.io.MatrixReader.java

protected static void sortSparseRowsParallel(MatrixBlock dest, long rlen, int k, ExecutorService pool)
        throws InterruptedException, ExecutionException {
    //create sort tasks (increase number of tasks for better load balance)
    ArrayList<SortRowsTask> tasks = new ArrayList<>();
    int k2 = (int) Math.min(8 * k, rlen);
    int blklen = (int) (Math.ceil((double) rlen / k2));
    for (int i = 0; i < k2 & i * blklen < rlen; i++)
        tasks.add(new SortRowsTask(dest, i * blklen, Math.min((i + 1) * blklen, (int) rlen)));

    //execute parallel sort and check for errors
    List<Future<Object>> rt2 = pool.invokeAll(tasks);
    for (Future<Object> task : rt2)
        task.get(); //error handling
}

From source file:com.joyent.manta.benchmark.Benchmark.java

/**
 * Method used to run a multi-threaded benchmark.
 *
 * @param method to measure// w w w .  ja v  a 2 s.  c  o m
 * @param path path to store benchmarking test data
 * @param iterations number of iterations to run
 * @param concurrency number of threads to run
 * @throws IOException thrown when we can't communicate with the server
 */
private static void multithreadedBenchmark(final String method, final String path, final int iterations,
        final int concurrency) throws IOException {
    final AtomicLong fullAggregation = new AtomicLong(0L);
    final AtomicLong serverAggregation = new AtomicLong(0L);
    final AtomicLong count = new AtomicLong(0L);
    final long perThreadCount = perThreadCount(iterations, concurrency);

    System.out.printf("Running %d iterations per thread\n", perThreadCount);

    final long testStart = System.nanoTime();

    Runtime.getRuntime().addShutdownHook(new Thread(Benchmark::cleanUp));

    final Callable<Void> worker = () -> {
        for (int i = 0; i < perThreadCount; i++) {
            Duration[] durations;

            if (method.equals("put")) {
                durations = measurePut(sizeInBytesOrNoOfDirs);
            } else if (method.equals("putDir")) {
                durations = measurePutDir(sizeInBytesOrNoOfDirs);
            } else {
                durations = measureGet(path);
            }

            long fullLatency = durations[0].toMillis();
            long serverLatency = durations[1].toMillis();
            fullAggregation.addAndGet(fullLatency);
            serverAggregation.addAndGet(serverLatency);

            System.out.printf("%s %d full=%dms, server=%dms, thread=%s\n", method, count.getAndIncrement(),
                    fullLatency, serverLatency, Thread.currentThread().getName());
        }

        return null;
    };

    final Thread.UncaughtExceptionHandler handler = (t, e) -> LOG.error("Error when executing benchmark", e);

    final AtomicInteger threadCounter = new AtomicInteger(0);
    ThreadFactory threadFactory = r -> {
        Thread t = new Thread(r);
        t.setDaemon(true);
        t.setUncaughtExceptionHandler(handler);
        t.setName(String.format("benchmark-%d", threadCounter.incrementAndGet()));

        return t;
    };

    ExecutorService executor = Executors.newFixedThreadPool(concurrency, threadFactory);

    List<Callable<Void>> workers = new ArrayList<>(concurrency);
    for (int i = 0; i < concurrency; i++) {
        workers.add(worker);
    }

    try {
        List<Future<Void>> futures = executor.invokeAll(workers);

        boolean completed = false;
        while (!completed) {
            try (Stream<Future<Void>> stream = futures.stream()) {
                completed = stream.allMatch((f) -> f.isDone() || f.isCancelled());

                if (!completed) {
                    Thread.sleep(CHECK_INTERVAL);
                }
            }
        }

    } catch (InterruptedException e) {
        return;
    } finally {
        System.err.println("Shutting down the thread pool");
        executor.shutdown();
    }

    final long testEnd = System.nanoTime();

    final long fullAverage = Math.round(fullAggregation.get() / iterations);
    final long serverAverage = Math.round(serverAggregation.get() / iterations);
    final long totalTime = Duration.ofNanos(testEnd - testStart).toMillis();

    System.out.printf("Average full latency: %d ms\n", fullAverage);
    System.out.printf("Average server latency: %d ms\n", serverAverage);
    System.out.printf("Total test time: %d ms\n", totalTime);
    System.out.printf("Total invocations: %d\n", count.get());
}