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

public static void stop(ExecutorService executor) {
    try {/*  ww  w .  java 2 s. c o  m*/
        executor.shutdown();
        executor.awaitTermination(60, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        System.err.println("termination interrupted");
    } finally {
        if (!executor.isTerminated()) {
            System.out.println("killing non-finished tasks");
        }
        executor.shutdownNow();
    }
}

From source file:Main.java

public static void shutdownAndWait(ExecutorService threadPool, long timeout, TimeUnit unit) {
    threadPool.shutdown();
    try {//from   w w  w .j a v  a  2  s. c o m
        threadPool.awaitTermination(timeout, unit);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void shutdown(final ExecutorService executorService) {
    executorService.shutdown();
    try {/*from   w w  w .j  ava 2 s. c o m*/
        int timeToWait = 30;
        if (!executorService.awaitTermination(timeToWait, TimeUnit.SECONDS)) {
            List<Runnable> executionList = executorService.shutdownNow();
            for (Runnable runnable : executionList) {
                System.out.println("Trying to shutdown task: " + runnable);
            }
        }
        if (!executorService.awaitTermination(timeToWait, TimeUnit.SECONDS)) {
        }
    } catch (InterruptedException ex) {
        executorService.shutdownNow();
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

/**
 * Uses the shutdown pattern from http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html
 * @param pool ExecutorService need to shutdown
 * @param period wait time period/*from  w  ww  .j a  va  2 s  . c  o m*/
 * @param unit wait time unit.
 */
public static void shutdownAndAwaitTermination(ExecutorService pool, long period, TimeUnit unit) {
    pool.shutdown(); // Disable new tasks from being submitted
    try {
        // Wait a while for existing tasks to terminate
        if (!pool.awaitTermination(period, unit)) {
            pool.shutdownNow(); // Cancel currently executing tasks
            // Wait a while for tasks to respond to being cancelled
            if (!pool.awaitTermination(period, unit))
                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 void gracefulShutdown(ExecutorService pool, int timeout, TimeUnit timeUnit) {
    pool.shutdown(); // Disable new tasks from being submitted
    try {//from w w  w . j  av  a  2 s .  com
        // Wait a while for existing tasks to terminate
        if (!pool.awaitTermination(timeout, timeUnit)) {
            pool.shutdownNow(); // Cancel currently executing tasks
            // Wait a while for tasks to respond to being cancelled
            if (!pool.awaitTermination(timeout, 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

/**
 * close the thread pool safely.//from  w  w w .j  a  va2s . c o m
 * @param pool 
 */
public static void safeClose(ExecutorService pool) {
    if (pool != null) {
        pool.shutdown();
        try {
            if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
                pool.shutdownNow();
            }
        } catch (InterruptedException ex) {
            //ignore the ex
        }
    }
}

From source file:Main.java

public static void shutdownAndAwaitTermination(ExecutorService pool) {
    if (pool != null) {
        pool.shutdown(); // Disable new tasks from being submitted
        try {/*from  w ww.j av  a2s.  c  o m*/
            // Wait a while for existing tasks to terminate
            if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
                pool.shutdownNow(); // Cancel currently executing tasks
                // Wait a while for tasks to respond to being cancelled
                if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
                    throw new InterruptedException("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 void gracefulShutdown(ExecutorService pool, int shutdownTimeout, int shutdownNowTimeout,
        TimeUnit timeUnit) {//from  w  ww  .  ja  v  a2  s  .c o m
    pool.shutdown();

    try {
        if (!pool.awaitTermination((long) shutdownTimeout, timeUnit)) {
            pool.shutdownNow();
            if (!pool.awaitTermination((long) shutdownNowTimeout, timeUnit)) {
                System.err.println("Pool did not terminated");
            }
        }
    } catch (InterruptedException var5) {
        pool.shutdownNow();
        Thread.currentThread().interrupt();
    }

}

From source file:Main.java

/**
 * Run the given runnable in a new thread.
 *
 * @param runnable The runnable to run in a new thread.
 *///from w  ww  .j a v a  2  s.  co  m
public static void inNewThread(Runnable runnable) {
    ExecutorService executor = Executors.newSingleThreadExecutor();

    executor.submit(runnable);

    executor.shutdown();
}

From source file:Main.java

public static void gracefulShutdown(ExecutorService pool, int shutdownTimeout, int shutdownNowTimeout,
        TimeUnit timeUnit) {//from   www.  j  a  v  a2 s.  c o m
    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 terminated");
            }
        }
    } catch (InterruptedException ie) {
        // (Re-)Cancel if current thread also interrupted
        pool.shutdownNow();
        // Preserve interrupt status
        Thread.currentThread().interrupt();
    }
}