Example usage for java.util.concurrent ExecutorService awaitTermination

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

Introduction

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

Prototype

boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;

Source Link

Document

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

Usage

From source file:gda.device.detector.mythen.data.MythenDataFileUtils.java

/**
 * Reads the specified Mythen processed data files.
 * /* w  w  w. j a v a2s. c  om*/
 * @param filenames
 *            the names of the files to read
 * @return 3D double array of data
 */
public static double[][][] readMythenProcessedDataFiles(String filenames[]) {
    // 3D array of data; will be filled in by tasks (one task per file to be loaded)
    final double[][][] data = new double[filenames.length][][];

    // thread pool for loading files
    // 4 threads seems to give good results
    ExecutorService executor = Executors.newFixedThreadPool(4);

    // create and execute a task for each file to be loaded
    for (int i = 0; i < filenames.length; i++) {
        final int index = i;
        final String filename = filenames[i];
        Runnable r = new Runnable() {
            @Override
            public void run() {
                data[index] = readMythenProcessedDataFile(filename, false);
            }

        };
        executor.execute(r);
    }

    // wait until executor has shut down
    executor.shutdown();
    try {
        boolean terminated = executor.awaitTermination(1, TimeUnit.MINUTES);
        if (!terminated) {
            throw new Exception("Timed out waiting for files to load");
        }
    } catch (Exception e) {
        throw new RuntimeException("Unable to load data", e);
    }

    return data;
}

From source file:org.apache.accumulo.test.functional.FunctionalTestUtils.java

public static void createRFiles(final AccumuloClient c, final FileSystem fs, String path, int rows, int splits,
        int threads) throws Exception {
    fs.delete(new Path(path), true);
    ExecutorService threadPool = Executors.newFixedThreadPool(threads);
    final AtomicBoolean fail = new AtomicBoolean(false);
    for (int i = 0; i < rows; i += rows / splits) {
        TestIngest.IngestParams params = new TestIngest.IngestParams(c.properties());
        params.outputFile = String.format("%s/mf%s", path, i);
        params.random = 56;//from   w  ww. j a v  a 2s .c o m
        params.timestamp = 1;
        params.dataSize = 50;
        params.rows = rows / splits;
        params.startRow = i;
        params.cols = 1;
        threadPool.execute(() -> {
            try {
                TestIngest.ingest(c, fs, params);
            } catch (Exception e) {
                fail.set(true);
            }
        });
    }
    threadPool.shutdown();
    threadPool.awaitTermination(1, TimeUnit.HOURS);
    assertFalse(fail.get());
}

From source file:net.wequick.small.Bundle.java

private static void loadBundles(List<Bundle> bundles) {
    sPreloadBundles = bundles;/*from  ww  w. j a v  a 2s  .c  om*/

    // Prepare bundle
    for (Bundle bundle : bundles) {
        bundle.prepareForLaunch();
    }

    // Handle I/O
    if (sIOActions != null) {
        ExecutorService executor = Executors.newFixedThreadPool(sIOActions.size());
        for (Runnable action : sIOActions) {
            executor.execute(action);
        }
        executor.shutdown();
        try {
            if (!executor.awaitTermination(LOADING_TIMEOUT_MINUTES, TimeUnit.MINUTES)) {
                throw new RuntimeException(
                        "Failed to load bundles! (TIMEOUT > " + LOADING_TIMEOUT_MINUTES + "minutes)");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        sIOActions = null;
    }

    // Notify `postSetUp' to all launchers
    for (BundleLauncher launcher : sBundleLaunchers) {
        launcher.postSetUp();
    }

    // Free all unused temporary variables
    for (Bundle bundle : bundles) {
        if (bundle.parser != null) {
            bundle.parser.close();
            bundle.parser = null;
        }
        bundle.mBuiltinFile = null;
        bundle.mExtractPath = null;
    }
}

From source file:org.apache.accumulo.server.util.VerifyTabletAssignments.java

private static void checkTable(final String user, final String pass, String table, HashSet<KeyExtent> check,
        boolean verbose)
        throws AccumuloException, AccumuloSecurityException, TableNotFoundException, InterruptedException {

    if (check == null)
        System.out.println("Checking table " + table);
    else// w w  w  .j  ava 2 s . co m
        System.out.println("Checking table " + table + " again, failures " + check.size());

    Map<KeyExtent, String> locations = new TreeMap<KeyExtent, String>();
    SortedSet<KeyExtent> tablets = new TreeSet<KeyExtent>();

    MetadataTable.getEntries(HdfsZooInstance.getInstance(),
            new AuthInfo(user, ByteBuffer.wrap(pass.getBytes()), HdfsZooInstance.getInstance().getInstanceID()),
            table, false, locations, tablets);

    final HashSet<KeyExtent> failures = new HashSet<KeyExtent>();

    for (KeyExtent keyExtent : tablets)
        if (!locations.containsKey(keyExtent))
            System.out.println(" Tablet " + keyExtent + " has no location");
        else if (verbose)
            System.out.println(" Tablet " + keyExtent + " is located at " + locations.get(keyExtent));

    Map<String, List<KeyExtent>> extentsPerServer = new TreeMap<String, List<KeyExtent>>();

    for (Entry<KeyExtent, String> entry : locations.entrySet()) {
        List<KeyExtent> extentList = extentsPerServer.get(entry.getValue());
        if (extentList == null) {
            extentList = new ArrayList<KeyExtent>();
            extentsPerServer.put(entry.getValue(), extentList);
        }

        if (check == null || check.contains(entry.getKey()))
            extentList.add(entry.getKey());
    }

    ExecutorService tp = Executors.newFixedThreadPool(20);

    for (final Entry<String, List<KeyExtent>> entry : extentsPerServer.entrySet()) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    checkTabletServer(user, ByteBuffer.wrap(pass.getBytes()), entry, failures);
                } catch (Exception e) {
                    System.err.println("Failure on ts " + entry.getKey() + " " + e.getMessage());
                    e.printStackTrace();
                    failures.addAll(entry.getValue());
                }
            }

        };

        tp.execute(r);
    }

    tp.shutdown();

    while (!tp.awaitTermination(1, TimeUnit.HOURS)) {
    }

    if (failures.size() > 0)
        checkTable(user, pass, table, failures, verbose);
}

From source file:fi.luontola.cqrshotel.framework.EventStoreContract.java

private static void repeatInParallel(int iterations, Runnable task, Runnable invariantChecker)
        throws Exception {
    final int PARALLELISM = 10;
    ExecutorService executor = Executors.newFixedThreadPool(PARALLELISM + 1);
    Future<?> checker;/*from w w  w  .  j a va  2s.co  m*/
    try {
        checker = executor.submit(() -> {
            while (!Thread.interrupted()) {
                invariantChecker.run();
                Thread.yield();
            }
        });
        List<Future<?>> futures = new ArrayList<>();
        for (int i = 0; i < iterations; i++) {
            futures.add(executor.submit(task));
        }
        for (Future<?> future : futures) {
            // will throw ExecutionException if there was a problem
            future.get();
        }
    } finally {
        executor.shutdownNow();
        executor.awaitTermination(10, TimeUnit.SECONDS);
    }
    // will throw ExecutionException if there was a problem
    checker.get(10, TimeUnit.SECONDS);
}

From source file:org.openhab.binding.network.service.NetworkService.java

/**
 * Starts the DiscoveryThread for each IP on the Networks
 *
 * @param allNetworkIPs/*from w  w w. j a  va2s .co  m*/
 */
private static void startDiscovery(final LinkedHashSet<String> networkIPs,
        final DiscoveryCallback discoveryCallback, ScheduledExecutorService scheduledExecutorService) {
    final int PING_TIMEOUT_IN_MS = 500;
    ExecutorService executorService = Executors
            .newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 10);

    for (Iterator<String> it = networkIPs.iterator(); it.hasNext();) {
        final String ip = it.next();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                if (ip != null) {
                    try {
                        if (Ping.checkVitality(ip, 0, PING_TIMEOUT_IN_MS)) {
                            discoveryCallback.newDevice(ip);
                        }
                    } catch (IOException e) {
                    }
                }
            }
        });
    }
    try {
        executorService.awaitTermination(PING_TIMEOUT_IN_MS * networkIPs.size(), TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
    }
    executorService.shutdown();
}

From source file:org.paxml.launch.PaxmlRunner.java

/**
 * Run the computed launch model with thread pool. It will run with default
 * 4 threads if not specifically specified in the launch model.
 * /*from  ww w .j  ava2 s .c  o  m*/
 * @param model
 *            the model containing the launch points
 */
public static void run(LaunchModel model, long executionId) {

    List<LaunchPoint> points = model.getLaunchPoints(false, executionId);
    if (log.isInfoEnabled()) {
        log.info("Found " + points.size() + " Paxml files to execute based on plan file: "
                + model.getPlanEntity().getResource().getPath());
    }
    if (points.isEmpty()) {
        return;
    }
    final int poolSize = model.getConcurrency() <= 0 ? Math.min(DEFAULT_CONCURRENCY, points.size())
            : model.getConcurrency();
    ExecutorService pool = Executors.newFixedThreadPool(poolSize);
    for (final LaunchPoint point : points) {
        pool.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    Context.cleanCurrentThreadContext();

                    logExecution(point.getResource(), true);

                    point.execute();
                } catch (Throwable t) {
                    if (log.isErrorEnabled()) {
                        log.error(findMessage(t), t);
                    }
                } finally {
                    logExecution(point.getResource(), false);
                }
            }

        });
    }

    try {
        pool.shutdown();
        // wait forever in a loop
        while (!pool.awaitTermination(1, TimeUnit.MINUTES)) {
            if (log.isDebugEnabled()) {
                log.debug("Waiting for all executors to finish...");
            }
        }

    } catch (InterruptedException e) {
        throw new PaxmlRuntimeException("Cannot wait for all executors to finish", e);
    } finally {
        pool.shutdownNow();
    }

}

From source file:ejp.examples.MultiThreadedWithConnectionPooling.java

static void execute(final DatabaseManager dbm) throws DatabaseException, InterruptedException {
    long time = System.currentTimeMillis();
    ExecutorService exec = Executors.newFixedThreadPool(100);

    System.out.println("\n\nWorking ...");

    Runnable runnable = new Runnable() {
        public void run() {
            for (int t = 0; t < 100; t++) {
                try {
                    new UpdateManager(dbm) {
                        public void run() throws DatabaseException {
                            for (int j = 0; j < 100; j++) {
                                saveObject(new Dog(String.valueOf(Count.get()), Count.get()));
                                Count.count();
                            }/*from   w  w  w .j  a v  a2 s . c  o m*/
                        }
                    }.executeBatchUpdates();
                } catch (DatabaseException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    for (int i = 0; i < 100; i++) {
        exec.execute(runnable);
    }

    exec.shutdown();
    exec.awaitTermination(100, TimeUnit.SECONDS);
    time = (System.currentTimeMillis() - time) / 1000;

    System.out.println("\n\n" + Count.count + " dogs added to database in " + time + " seconds");

    Long count = ((Collection<Long>) dbm.executeQuery(new ArrayList<Long>(), true, "select count(*) from dog"))
            .toArray(new Long[1])[0];
    System.out.println("select count(*) from dog = " + count);
}

From source file:io.pcp.parfait.benchmark.CPUThreadTest.java

private void awaitExecutionCompletion(ExecutorService executorService) {
    try {/*w w  w . ja  v a  2  s.c o  m*/
        executorService.shutdown();
        executorService.awaitTermination(1, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:org.geowebcache.sqlite.SqlitlePerf.java

/**
 * Select the created tiles using a raw connection.
 *///from   w  w  w .  ja  v a 2 s . co m
private static void rawSqlitle(File rootDirectory, File seedFile, long[][] tiles) throws Exception {
    // creating a new database by copying the seeded one
    File databaseFile = new File(rootDirectory, "raw_perf_test.sqlite");
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info(String.format("Start raw select from file '%s'.", databaseFile));
    }
    FileUtils.copyFile(seedFile, databaseFile);
    // submitting the select tasks
    ExecutorService executor = Executors.newFixedThreadPool(WORKERS);
    long startTime = System.currentTimeMillis();
    Connection connection = DriverManager.getConnection("jdbc:sqlite:" + seedFile.getPath());
    for (int i = 0; i < tiles.length; i++) {
        long[] tile = tiles[i];
        executor.submit((Runnable) () -> getTile(connection, tile));
        if (i != 0 && i % 10000 == 0) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(String.format("Submitted %d select tasks.", i));
            }
        }
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(String.format("Submitted %d select tasks.", TILES));
    }
    // lets wait for the workers to finish
    executor.shutdown();
    executor.awaitTermination(5, TimeUnit.MINUTES);
    // computing some stats
    long endTime = System.currentTimeMillis();
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info(String.format("Tiles raw select time '%d'.", endTime - startTime));
    }
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info(String.format("Tiles raw selected per second '%f'.",
                TILES / (float) (endTime - startTime) * 1000));
    }
    // clean everything
    connection.close();
    FileUtils.deleteQuietly(databaseFile);
}