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:Main.java

public static void shutdown(ExecutorService executor) {
    if (executor != null) {
        executor.shutdownNow();
        // executor.awaitTermination(1, TimeUnit.SECONDS);
        executor = null;//ww w .  j a v  a 2 s .  co m
    }
}

From source file:Main.java

public static void normalShutdown(ExecutorService pool, int timeout, TimeUnit timeUnit) {
    try {/*  w  w  w  . j av a 2  s .  c  o m*/
        pool.shutdownNow();
        if (!pool.awaitTermination(timeout, timeUnit)) {
            System.err.println("Pool did not terminated");
        }
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

/**
 * Normal shutdown./*from   w ww  . j  av  a2  s .  co m*/
 *
 * @param pool the pool
 * @param timeout the timeout
 * @param timeUnit the time unit
 */
public static void normalShutdown(ExecutorService pool, int timeout, TimeUnit timeUnit) {
    try {
        pool.shutdownNow();
        if (!pool.awaitTermination(timeout, timeUnit)) {
            System.err.println("Pool did not terminate");
        }
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

public static void shutdownNow(ExecutorService exec) {
    if (exec != null) {
        List<Runnable> tasks = exec.shutdownNow();

        if (tasks.size() == 0) {
            System.out.println(/*from  www.j  ava  2  s . c  om*/
                    "Runnable tasks outlived thread pool executor service [" + ", tasks=" + tasks + ']');
        }

        try {
            exec.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            System.out.println(
                    "Got interrupted while waiting for executor service to stop.[" + e.toString() + "]");
        }
    }
}

From source file:Main.java

/**
 * close the thread pool safely.//from w  w w  . j  a  va  2  s.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 addExecutorShutdownHook(final ExecutorService exec, final long time,
        final TimeUnit timeUnit) {
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        public void run() {
            exec.shutdownNow();
        }/* w  w w. j ava2s.c om*/
    }));
}

From source file:Main.java

public static void gracefulShutdown(ExecutorService pool, int shutdownTimeout, int shutdownNowTimeout,
        TimeUnit timeUnit) {//www  . j  a va 2 s . com
    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

public static void shutdown(final ExecutorService executorService) {
    executorService.shutdown();//from w w  w .  ja v  a  2s .c o m
    try {
        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

public static final void shutdownAndAwaitTermination(ExecutorService executorService) {
    if (executorService.isShutdown()) {
        return;//  www  . j ava 2 s . c  om
    }
    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

/**
 * 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 ww  w . j  a v  a2  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();
    }
}