Example usage for java.util.concurrent ExecutorService shutdown

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

Introduction

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

Prototype

void shutdown();

Source Link

Document

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.

Usage

From source file:Main.java

/**
 * Attempts to gracefully shutdown the given {@link ExecutorService}.
 *
 * @param threadPool the {@code ExecutorService} to shutdown
 *//*from ww  w.  j a v  a  2s . c  o  m*/
public static void shutdownAndAwaitTermination(ExecutorService threadPool) {
    if (threadPool == null)
        return;
    threadPool.shutdown(); // Disable new tasks from being submitted
    try {
        // Wait a while for existing tasks to terminate
        if (!threadPool.awaitTermination(30, TimeUnit.SECONDS)) {
            threadPool.shutdownNow(); // Cancel currently executing tasks
            // Wait a while for tasks to respond to being cancelled
            if (!threadPool.awaitTermination(30, TimeUnit.SECONDS))
                System.err.println("Pool did not terminate");
        }
    } catch (InterruptedException ie) {
        // (Re-)Cancel if current thread also interrupted
        threadPool.shutdownNow();
        // Preserve interrupt status
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

/**
 * Graceful shutdown./* w w w.j a va 2 s. c  om*/
 *
 * @param pool the pool
 * @param shutdownTimeout the shutdown timeout
 * @param shutdownNowTimeout the shutdown now timeout
 * @param timeUnit the time unit
 */
public static void gracefulShutdown(ExecutorService pool, int shutdownTimeout, int shutdownNowTimeout,
        TimeUnit timeUnit) {
    pool.shutdown(); // Disable new tasks from being submitted
    try {
        // Wait a while for existing tasks to terminate
        if (!pool.awaitTermination(shutdownTimeout, timeUnit)) {
            pool.shutdownNow(); // Cancel currently executing tasks
            // Wait a while for tasks to respond to being cancelled
            if (!pool.awaitTermination(shutdownNowTimeout, timeUnit)) {
                System.err.println("Pool did not terminate");
            }
        }
    } catch (InterruptedException ie) {
        // (Re-)Cancel if current thread also interrupted
        pool.shutdownNow();
        // Preserve interrupt status
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

public static final void shutdownAndAwaitTermination(ExecutorService executorService) {
    if (executorService.isShutdown()) {
        return;//from   ww  w .j  ava 2 s.  c o m
    }
    executorService.shutdown();
    try {
        if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
            executorService.shutdownNow();
        }
    } catch (InterruptedException ie) {
        executorService.shutdownNow();
        Thread.currentThread().interrupt();
    }
    executorService.shutdownNow();

}

From source file:Main.java

public static void runAllServices(Runnable... runnables) {
    ExecutorService service = Executors.newCachedThreadPool();
    for (Runnable runnable : runnables) {
        service.submit(runnable);//from www.  j  av a  2  s .  co  m
    }
    service.shutdown();
}

From source file:Main.java

/**
 * Properly shutdown and await pool termination for an arbitrary
 * amount of time./*from  w  w w.j av a2s .c o  m*/
 *
 * @param pool        Pool to shutdown
 * @param waitSeconds Seconds to wait before throwing exception
 * @throws java.util.concurrent.TimeoutException Thrown if the pool does not shutdown in the specified time
 */
public static void shutdownAndWaitForTermination(ExecutorService pool, int waitSeconds)
        throws TimeoutException {
    // shut it down
    pool.shutdown();
    try {
        // wait, wait, wait
        if (!pool.awaitTermination(waitSeconds, TimeUnit.SECONDS)) {
            // things still running, nuke it
            pool.shutdownNow();
            // wait, wait, wai
            if (!pool.awaitTermination(waitSeconds, TimeUnit.SECONDS)) {
                // boo hiss, didn't shutdown
                throw new TimeoutException("Pool did not terminate");
            }
        }
    } catch (InterruptedException ie) {
        // try, try again
        pool.shutdownNow();
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

public static final void shutdownAndAwaitTermination(ExecutorService executorService, long timeout,
        TimeUnit timeUnit) {/*from   w w  w.  jav a  2  s .co  m*/
    if (isShutDown(executorService)) {
        return;
    }

    executorService.shutdown();
    try {
        if (!executorService.awaitTermination(timeout, timeUnit)) {
            executorService.shutdownNow();
        }
    } catch (InterruptedException ie) {
        executorService.shutdownNow();
        Thread.currentThread().interrupt();
    }
    executorService.shutdownNow();

}

From source file:Main.java

public static final void shutdownAndAwaitTermination(ExecutorService executorService, long timeout,
        TimeUnit timeUnit) {//from  ww w  . j a  va  2  s. c om
    if (executorService.isShutdown()) {
        return;
    }
    executorService.shutdown();
    try {
        if (!executorService.awaitTermination(timeout, timeUnit)) {
            executorService.shutdownNow();
        }
    } catch (InterruptedException ie) {
        executorService.shutdownNow();
        Thread.currentThread().interrupt();
    }
    executorService.shutdownNow();

}

From source file:Main.java

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

From source file:gobblin.aws.GobblinAWSUtils.java

/***
 * Initiates an orderly shutdown in which previously submitted
 * tasks are executed, but no new tasks are accepted.
 * Invocation has no additional effect if already shut down.
 *
 * This also blocks until all tasks have completed execution
 * request, or the timeout occurs, or the current thread is
 * interrupted, whichever happens first.
 * @param clazz {@link Class} that invokes shutdown on the {@link ExecutorService}.
 * @param executorService {@link ExecutorService} to shutdown.
 * @param logger {@link Logger} to log shutdown for invoking class.
 * @throws InterruptedException if shutdown is interrupted.
 *//*  w  ww  . ja v  a2  s.  com*/
public static void shutdownExecutorService(Class clazz, ExecutorService executorService, Logger logger)
        throws InterruptedException {
    executorService.shutdown();
    if (!executorService.awaitTermination(DEFAULT_EXECUTOR_SERVICE_SHUTDOWN_TIME_IN_MINUTES,
            TimeUnit.MINUTES)) {
        logger.warn("Executor service shutdown timed out.");
        List<Runnable> pendingTasks = executorService.shutdownNow();
        logger.warn(String.format("%s was shutdown instantly. %s tasks were not executed: %s", clazz.getName(),
                pendingTasks.size(), StringUtils.join(pendingTasks, ",")));
    }
}

From source file:Main.java

static <T> List<T> submit(List<? extends Callable<T>> taskList, final String taskName) throws Exception {

    List<Future<T>> futureList = new ArrayList();

    List<T> result = new ArrayList<>();

    ExecutorService pool = newFixedThreadPool(taskList.size(), taskName);

    for (Callable<T> callable : taskList) {
        futureList.add(pool.submit(callable));
    }/*from  w w w .  j a va  2 s .c om*/

    pool.shutdown();

    for (Future<T> f : futureList) {
        try {
            result.add(f.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return result;
}