Example usage for java.util.concurrent ExecutorService awaitTermination

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

Introduction

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

Prototype

boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;

Source Link

Document

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

Usage

From source file:Main.java

public static void shutdown(final ExecutorService executorService) {
    executorService.shutdown();//from   ww w  .jav  a2s.  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 void gracefulShutdown(ExecutorService pool, int shutdownTimeout, int shutdownNowTimeout,
        TimeUnit timeUnit) {/*from  www  . j  a v a 2 s  .c om*/
    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 final void shutdownAndAwaitTermination(ExecutorService executorService) {
    if (executorService.isShutdown()) {
        return;/*from  w w  w  .  ja  v  a  2  s .  com*/
    }
    executorService.shutdown();
    try {
        if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
            executorService.shutdownNow();
        }
    } catch (InterruptedException ie) {
        executorService.shutdownNow();
        Thread.currentThread().interrupt();
    }
    executorService.shutdownNow();

}

From source file:org.thelq.stackexchange.dbimport.Utils.java

public static void shutdownPool(ExecutorService pool, String poolName) throws InterruptedException {
    pool.shutdown();/*from  w w w  . j a v a2s  .c  o  m*/
    int secondsPassed = 0;
    while (pool.awaitTermination(5, TimeUnit.SECONDS) == false) {
        secondsPassed = secondsPassed + 5;
        log.info("Still waiting for " + poolName + " to " + secondsPassed);
    }
}

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/*  w  w w. ja  v  a 2s.  c om*/
 * @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 {// www  .  ja  va  2 s  . co 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

/**
 * Properly shutdown and await pool termination for an arbitrary
 * amount of time.//from  w  ww  .j a  v a  2s.  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: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//from w  w w  .j a  va 2 s  .c  om
 * @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) {//from ww w  .  j av  a 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:Main.java

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

}