com.fireflysource.common.concurrent.ExecutorServiceUtils Maven / Gradle / Ivy
package com.fireflysource.common.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
/**
* The executor service common utilities.
*
* @author Pengtao Qiu
*/
abstract public class ExecutorServiceUtils {
/**
* Blocks until all tasks have completed execution after a shutdown
* request, or the timeout occurs, or the current thread is
* interrupted, whichever happens first.
*
* @param pool The thread pool that will shutdown.
* @param timeout The maximum time to wait.
* @param unit The time unit of the timeout argument.
*/
public static void shutdownAndAwaitTermination(ExecutorService pool, long timeout, TimeUnit unit) {
try {
// Disable new tasks from being submitted
pool.shutdown();
// Wait a while for existing tasks to terminate
if (!pool.awaitTermination(timeout, unit)) {
pool.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!pool.awaitTermination(timeout, 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();
} catch (Exception e) {
e.printStackTrace();
}
}
}