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