List of usage examples for java.util.concurrent ExecutorService awaitTermination
boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;
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. *///from w ww. j ava 2 s . co m 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
public static void shutdownAndAwaitTermination(ExecutorService pool) { if (pool != null) { pool.shutdown(); // Disable new tasks from being submitted try {/* w w w.jav a 2 s .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 ww w . j a v a 2s.c om*/ 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: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/* w ww . j a v a2 s .c om*/ * @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; } }
From source file:Main.java
/** * Attempts to gracefully shutdown the given {@link ExecutorService}. * * @param threadPool the {@code ExecutorService} to shutdown *//* w ww . j a va 2 s . c om*/ 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.//from www . j a v a 2 s .c o m * * @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 void runRunnablesNTimesAndAwaitCompletion(int times, final List<Runnable> runnables) throws InterruptedException { ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(runnables.size()); for (Runnable r : runnables) { for (int i = 0; i < times; i++) { newFixedThreadPool.execute(r); }/*from w ww. j av a2s .co m*/ } newFixedThreadPool.shutdown(); newFixedThreadPool.awaitTermination(5, TimeUnit.SECONDS); }
From source file:org.apache.phoenix.util.PhoenixMRJobUtil.java
public static void shutdown(ExecutorService pool) throws InterruptedException { pool.shutdown();/*from w w w. j ava 2s.c o m*/ LOG.debug("Shutdown called"); pool.awaitTermination(200, TimeUnit.MILLISECONDS); LOG.debug("Await termination called to wait for 200 msec"); if (!pool.isShutdown()) { pool.shutdownNow(); LOG.debug("Await termination called to wait for 200 msec"); pool.awaitTermination(100, TimeUnit.MILLISECONDS); } if (!pool.isShutdown()) { LOG.warn("Pool did not shutdown"); } }
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 ww w . j ava 2 s . co m*/ "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:com.uniteddev.Unity.Downloader.java
public static void renameFiles() throws IOException, InterruptedException { Login.progressText.setText("Renaming files..."); System.out.println("Renaming files..."); class renameFile implements Runnable { private int i; renameFile(int i) { this.i = i; }// w w w. j av a2s . co m public void run() { //System.out.println("Currently attempting Pool Index: "+this.i); String file = rename_files.get(this.i); if (file.contains("%20") || file.contains("%5b") || file.contains("%5d")) { file = namefix(file); File oldfile = new File(Minecraft.getWorkingDirectory(), rename_files.get(this.i)); if (oldfile.exists()) { File newfile = new File(Minecraft.getWorkingDirectory(), file); oldfile.renameTo(newfile); System.out.println("Renamed " + oldfile.getName() + " to " + newfile.getName()); } } } } ExecutorService pool = Executors.newFixedThreadPool(cores + 4); for (int i = 0; i < rename_files.size(); i++) { pool.submit(new renameFile(i)); } pool.shutdown(); pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS); }