List of usage examples for java.util.concurrent ExecutorService shutdownNow
List<Runnable> shutdownNow();
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 ww w . j a va 2 s. c o m*/ // 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
public static final void shutdownAndAwaitTermination(ExecutorService executorService, long timeout, TimeUnit timeUnit) {// www. j ava 2 s . c o 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:org.apache.streams.util.ComponentUtils.java
/** * Attempts to safely {@link java.util.concurrent.ExecutorService#shutdown()} and {@link java.util.concurrent.ExecutorService#awaitTermination(long, java.util.concurrent.TimeUnit)} * of an {@link java.util.concurrent.ExecutorService}. * @param stream service to be shutdown//w w w . jav a 2 s . c o m * @param initialWait time in seconds to wait for currently running threads to finish execution * @param secondaryWait time in seconds to wait for running threads that did not terminate in the first wait to acknowledge their forced termination */ public static void shutdownExecutor(ExecutorService stream, int initialWait, int secondaryWait) { stream.shutdown(); try { if (!stream.awaitTermination(initialWait, TimeUnit.SECONDS)) { stream.shutdownNow(); if (!stream.awaitTermination(secondaryWait, TimeUnit.SECONDS)) { LOGGER.error("Executor Service did not terminate"); } } } catch (InterruptedException ie) { stream.shutdownNow(); Thread.currentThread().interrupt(); } }
From source file:Main.java
public static final void shutdownAndAwaitTermination(ExecutorService executorService, long timeout, TimeUnit timeUnit) {/* w w w . j a va 2 s .co m*/ 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 shutdownAndAwaitTermination(ExecutorService pool) { if (pool != null) { pool.shutdown(); // Disable new tasks from being submitted try {/*w w w .ja v a 2 s . co 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 .j a v a2 s . co 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(); } }
From source file:Main.java
/** * Attempts to gracefully shutdown the given {@link ExecutorService}. * * @param threadPool the {@code ExecutorService} to shutdown *///from w w w .j a va2s . 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./*ww w.j a va 2s . com*/ * * @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
/** * Properly shutdown and await pool termination for an arbitrary * amount of time.//from w ww . j a v a 2 s.co 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:com.buaa.cfs.utils.ShutdownThreadsHelper.java
/** * @param service {@link ExecutorService to be shutdown} * @param timeoutInMs time to wait for {@link * ExecutorService#awaitTermination(long, TimeUnit)} * calls in milli seconds. * @return <tt>true</tt> if the service is terminated, * <tt>false</tt> otherwise//from ww w . j av a2s .c o m * @throws InterruptedException */ public static boolean shutdownExecutorService(ExecutorService service, long timeoutInMs) throws InterruptedException { if (service == null) { return true; } service.shutdown(); if (!service.awaitTermination(timeoutInMs, TimeUnit.MILLISECONDS)) { service.shutdownNow(); return service.awaitTermination(timeoutInMs, TimeUnit.MILLISECONDS); } else { return true; } }