Example usage for java.util.concurrent ExecutorService shutdownNow

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

Introduction

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

Prototype

List<Runnable> shutdownNow();

Source Link

Document

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

Usage

From source file:org.apache.sentry.api.common.SentryServiceUtil.java

/**
 * Gracefully shut down an Executor service.
 * <p>/*w  w w.  ja v  a2 s .c o  m*/
 * This code is based on the Javadoc example for the Executor service.
 * <p>
 * First call shutdown to reject incoming tasks, and then call
 * shutdownNow, if necessary, to cancel any lingering tasks.
 *
 * @param pool the executor service to shut down
 * @param poolName the name of the executor service to shut down to make it easy for debugging
 * @param timeout the timeout interval to wait for its termination
 * @param unit the unit of the timeout
 * @param logger the logger to log the error message if it cannot terminate. It could be null
 */
public static void shutdownAndAwaitTermination(ExecutorService pool, String poolName, long timeout,
        TimeUnit unit, Logger logger) {
    Preconditions.checkNotNull(pool);

    pool.shutdown(); // Disable new tasks from being submitted
    try {
        // Wait a while for existing tasks to terminate
        if (!pool.awaitTermination(timeout, unit)) {
            pool.shutdownNow(); // Cancel currently executing tasks
            // Wait a while for tasks to respond to being cancelled
            if ((!pool.awaitTermination(timeout, unit)) && (logger != null)) {
                logger.error("Executor service {} did not terminate",
                        StringUtils.defaultIfBlank(poolName, "null"));
            }
        }
    } catch (InterruptedException ignored) {
        // (Re-)Cancel if current thread also interrupted
        pool.shutdownNow();
        // Preserve interrupt status
        Thread.currentThread().interrupt();
    }
}

From source file:org.zenoss.zep.impl.Application.java

private static void stopExecutor(ExecutorService executorService) {
    executorService.shutdown();//from w  w  w.  j av a2 s .co m
    try {
        executorService.awaitTermination(0L, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        executorService.shutdownNow();
    }
}

From source file:edu.iu.kmeans.regroupallgather.KMUtil.java

/**
 * Generate data and upload to the data dir.
 * /*  ww w  .  j  av a 2 s  . com*/
 * @param numOfDataPoints
 * @param vectorSize
 * @param numPointFiles
 * @param localInputDir
 * @param fs
 * @param dataDir
 * @throws IOException
 * @throws InterruptedException
 * @throws ExecutionException
 */
static void generatePoints(int numOfDataPoints, int vectorSize, int numPointFiles, String localInputDir,
        FileSystem fs, Path dataDir) throws IOException, InterruptedException, ExecutionException {
    int pointsPerFile = numOfDataPoints / numPointFiles;
    System.out.println("Writing " + pointsPerFile + " vectors to a file");
    // Check data directory
    if (fs.exists(dataDir)) {
        fs.delete(dataDir, true);
    }
    // Check local directory
    File localDir = new File(localInputDir);
    // If existed, regenerate data
    if (localDir.exists() && localDir.isDirectory()) {
        for (File file : localDir.listFiles()) {
            file.delete();
        }
        localDir.delete();
    }
    boolean success = localDir.mkdir();
    if (success) {
        System.out.println("Directory: " + localInputDir + " created");
    }
    if (pointsPerFile == 0) {
        throw new IOException("No point to write.");
    }
    // Create random data points
    int poolSize = Runtime.getRuntime().availableProcessors();
    ExecutorService service = Executors.newFixedThreadPool(poolSize);
    List<Future<?>> futures = new LinkedList<Future<?>>();
    for (int k = 0; k < numPointFiles; k++) {
        Future<?> f = service
                .submit(new DataGenRunnable(pointsPerFile, localInputDir, Integer.toString(k), vectorSize));
        futures.add(f); // add a new thread
    }
    for (Future<?> f : futures) {
        f.get();
    }
    // Shut down the executor service so that this
    // thread can exit
    service.shutdownNow();
    // Wrap to path object
    Path localInput = new Path(localInputDir);
    fs.copyFromLocalFile(localInput, dataDir);
}

From source file:com.eucalyptus.blockstorage.TGTWrapper.java

public static void stop() {
    serviceLock.writeLock().lock();//from w  ww  .  ja v  a  2s  .  c  om
    try {
        ExecutorService toShutdown = service;
        service = null;
        toShutdown.shutdownNow();
    } catch (Exception e) {
        LOG.warn("Unable to shutdown thread pool", e);
    } finally {
        serviceLock.writeLock().unlock();
    }
}

From source file:edu.iu.daal_kmeans.regroupallgather.KMUtil.java

/**
 * Generate data and upload to the data dir.
 * /*from  w  w  w . j  a v a 2  s .c  o m*/
 * @param numOfDataPoints
 * @param vectorSize
 * @param numPointFiles
 * @param localInputDir
 * @param fs
 * @param dataDir
 * @throws IOException
 * @throws InterruptedException
 * @throws ExecutionException
 */
static void generatePoints(int numOfDataPoints, int vectorSize, int numPointFiles, String localInputDir,
        FileSystem fs, Path dataDir) throws IOException, InterruptedException, ExecutionException {
    int pointsPerFile = numOfDataPoints / numPointFiles;
    System.out.println("Writing " + pointsPerFile + " vectors to a file");
    // Check data directory
    if (fs.exists(dataDir)) {
        fs.delete(dataDir, true);
    }
    // Check local directory
    File localDir = new File(localInputDir);
    // If existed, regenerate data
    if (localDir.exists() && localDir.isDirectory()) {
        for (File file : localDir.listFiles()) {
            file.delete();
        }
        localDir.delete();
    }
    boolean success = localDir.mkdir();
    if (success) {
        System.out.println("Directory: " + localInputDir + " created");
    }
    if (pointsPerFile == 0) {
        throw new IOException("No point to write.");
    }
    // Create random data points
    int poolSize = Runtime.getRuntime().availableProcessors();
    ExecutorService service = Executors.newFixedThreadPool(poolSize);
    List<Future<?>> futures = new LinkedList<Future<?>>();
    for (int k = 0; k < numPointFiles; k++) {

        Future<?> f = service
                .submit(new DataGenMMDense(pointsPerFile, localInputDir, Integer.toString(k), vectorSize));

        futures.add(f); // add a new thread
    }
    for (Future<?> f : futures) {
        f.get();
    }
    // Shut down the executor service so that this
    // thread can exit
    service.shutdownNow();
    // Wrap to path object
    Path localInput = new Path(localInputDir);
    fs.copyFromLocalFile(localInput, dataDir);
    DeleteFileFolder(localInputDir);
}

From source file:edu.iu.daal_naive.NaiveUtil.java

static void generatePoints(int numOfDataPoints, int vectorSize, int numPointFiles, int nClasses,
        String localInputDir, FileSystem fs, Path dataDir)
        throws IOException, InterruptedException, ExecutionException {

    int pointsPerFile = numOfDataPoints / numPointFiles;
    System.out.println("Writing " + pointsPerFile + " vectors to a file");
    // Check data directory
    if (fs.exists(dataDir)) {
        fs.delete(dataDir, true);//from   ww  w.  j a  va2 s  .  co  m
    }
    // Check local directory
    File localDir = new File(localInputDir);
    // If existed, regenerate data
    if (localDir.exists() && localDir.isDirectory()) {
        for (File file : localDir.listFiles()) {
            file.delete();
        }
        localDir.delete();
    }
    boolean success = localDir.mkdir();
    if (success) {
        System.out.println("Directory: " + localInputDir + " created");
    }
    if (pointsPerFile == 0) {
        throw new IOException("No point to write.");
    }
    // Create random data points
    int poolSize = Runtime.getRuntime().availableProcessors();
    ExecutorService service = Executors.newFixedThreadPool(poolSize);
    List<Future<?>> futures = new LinkedList<Future<?>>();
    for (int k = 0; k < numPointFiles; k++) {
        Future<?> f = service.submit(
                new DataGenNaiveBayes(localInputDir, Integer.toString(k), pointsPerFile, vectorSize, nClasses));
        futures.add(f); // add a new thread
    }
    for (Future<?> f : futures) {
        f.get();
    }
    // Shut down the executor service so that this
    // thread can exit
    service.shutdownNow();
    // Wrap to path object
    Path localInput = new Path(localInputDir);
    fs.copyFromLocalFile(localInput, dataDir);
}

From source file:edu.iu.daal_pca.PCAUtil.java

/**
 * Generate data and upload to the data dir.
 *
 * @param numOfDataPoints/*w w w  . ja v  a 2 s .com*/
 * @param vectorSize
 * @param numPointFiles
 * @param localInputDir
 * @param fs
 * @param dataDir
 * @throws IOException
 * @throws InterruptedException
 * @throws ExecutionException
 */
static void generatePoints(int numOfDataPoints, int vectorSize, int numPointFiles, String localInputDir,
        FileSystem fs, Path dataDir) throws IOException, InterruptedException, ExecutionException {
    int pointsPerFile = numOfDataPoints / numPointFiles;
    System.out.println("Writing " + pointsPerFile + " vectors to a file");
    // Check data directory
    if (fs.exists(dataDir)) {
        fs.delete(dataDir, true);
    }
    // Check local directory
    File localDir = new File(localInputDir);
    // If existed, regenerate data
    if (localDir.exists() && localDir.isDirectory()) {
        for (File file : localDir.listFiles()) {
            file.delete();
        }
        localDir.delete();
    }
    boolean success = localDir.mkdir();
    if (success) {
        System.out.println("Directory: " + localInputDir + " created");
    }
    if (pointsPerFile == 0) {
        throw new IOException("No point to write.");
    }
    // Create random data points
    int poolSize = Runtime.getRuntime().availableProcessors();
    ExecutorService service = Executors.newFixedThreadPool(poolSize);
    List<Future<?>> futures = new LinkedList<Future<?>>();
    for (int k = 0; k < numPointFiles; k++) {
        // Future<?> f = service.submit(new DataGenRunnable(pointsPerFile, localInputDir, Integer.toString(k), vectorSize));
        Future<?> f = service
                .submit(new DataGenMMDense(pointsPerFile, localInputDir, Integer.toString(k), vectorSize));
        futures.add(f); // add a new thread
    }
    for (Future<?> f : futures) {
        f.get();
    }
    // Shut down the executor service so that this
    // thread can exit
    service.shutdownNow();
    // Wrap to path object
    Path localInput = new Path(localInputDir);
    fs.copyFromLocalFile(localInput, dataDir);
    DeleteFileFolder(localInputDir);
}

From source file:edu.iu.daal_svd.SVDUtil.java

/**
 * Generate data and upload to the data dir.
 * //from w  w  w .j a v  a  2s.  com
 * @param numOfDataPoints
 * @param vectorSize
 * @param numPointFiles
 * @param localInputDir
 * @param fs
 * @param dataDir
 * @throws IOException
 * @throws InterruptedException
 * @throws ExecutionException
 */
static void generatePoints(int numOfDataPoints, int vectorSize, int numPointFiles, String localInputDir,
        FileSystem fs, Path dataDir) throws IOException, InterruptedException, ExecutionException {
    int pointsPerFile = numOfDataPoints / numPointFiles;
    System.out.println("Writing " + pointsPerFile + " vectors to a file");
    // Check data directory
    if (fs.exists(dataDir)) {
        fs.delete(dataDir, true);
    }
    // Check local directory
    File localDir = new File(localInputDir);
    // If existed, regenerate data
    if (localDir.exists() && localDir.isDirectory()) {
        for (File file : localDir.listFiles()) {
            file.delete();
        }
        localDir.delete();
    }
    boolean success = localDir.mkdir();
    if (success) {
        System.out.println("Directory: " + localInputDir + " created");
    }
    if (pointsPerFile == 0) {
        throw new IOException("No point to write.");
    }
    // Create random data points
    int poolSize = Runtime.getRuntime().availableProcessors();
    ExecutorService service = Executors.newFixedThreadPool(poolSize);
    List<Future<?>> futures = new LinkedList<Future<?>>();
    for (int k = 0; k < numPointFiles; k++) {
        // Future<?> f =
        //   service.submit(new DataGenRunnable(
        //     pointsPerFile, localInputDir, Integer
        //       .toString(k), vectorSize));
        Future<?> f = service
                .submit(new DataGenMMDense(pointsPerFile, localInputDir, Integer.toString(k), vectorSize));

        futures.add(f); // add a new thread
    }
    for (Future<?> f : futures) {
        f.get();
    }
    // Shut down the executor service so that this
    // thread can exit
    service.shutdownNow();
    // Wrap to path object
    Path localInput = new Path(localInputDir);
    fs.copyFromLocalFile(localInput, dataDir);
    DeleteFileFolder(localInputDir);
}

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  ww. j  av  a 2  s .com*/
    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:sadl.utils.IoUtils.java

public static Pair<TimedInput, TimedInput> readTrainTestFile(Path trainTestFile,
        Function<Reader, TimedInput> f) {
    try (BufferedReader br = Files.newBufferedReader(trainTestFile);
            PipedWriter trainWriter = new PipedWriter();
            PipedReader trainReader = new PipedReader(trainWriter);
            PipedWriter testWriter = new PipedWriter();
            PipedReader testReader = new PipedReader(testWriter)) {
        String line = "";
        final ExecutorService ex = Executors.newFixedThreadPool(2);
        final Future<TimedInput> trainWorker = ex.submit(() -> f.apply(trainReader));
        final Future<TimedInput> testWorker = ex.submit(() -> f.apply(testReader));
        ex.shutdown();//from   ww w  .j  a  v a  2 s. co  m
        boolean writeTrain = true;
        while ((line = br.readLine()) != null) {
            if (line.startsWith(SmacDataGenerator.TRAIN_TEST_SEP)) {
                writeTrain = false;
                trainWriter.close();
                continue;
            }
            if (writeTrain) {
                trainWriter.write(line);
                trainWriter.write('\n');
            } else {
                testWriter.write(line);
                testWriter.write('\n');
            }
        }
        testWriter.close();
        ex.shutdown();
        if (writeTrain) {
            trainWriter.close();
            ex.shutdownNow();
            throw new IOException("The provided file " + trainTestFile + " does not contain the separator "
                    + SmacDataGenerator.TRAIN_TEST_SEP);
        }
        final Pair<TimedInput, TimedInput> result = Pair.of(trainWorker.get(), testWorker.get());
        return result;
    } catch (final IOException | InterruptedException | ExecutionException e) {
        logger.error("Unexpected exception!", e);
    }
    return null;
}