Java tutorial
//package com.java2s; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; public class Main { private static final int defaultTimeout = 60; public static final void shutdownAndAwaitTermination(ExecutorService executorService) { shutdownAndAwaitTermination(executorService, defaultTimeout, TimeUnit.SECONDS); } public static final void shutdownAndAwaitTermination(ExecutorService executorService, long timeout, TimeUnit timeUnit) { if (isShutDown(executorService)) { return; } executorService.shutdown(); try { if (!executorService.awaitTermination(timeout, timeUnit)) { executorService.shutdownNow(); } } catch (InterruptedException ie) { executorService.shutdownNow(); Thread.currentThread().interrupt(); } executorService.shutdownNow(); } public static boolean isShutDown(ExecutorService executorService) { return (executorService == null || executorService.isShutdown()); } }