Code example for ExecutorService

Methods: awaitTermination, shutdown, shutdownNow

0
package com.digitalxyncing.util; 
 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.TimeUnit; 
 
public class ThreadUtils { 
 
    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();
        } 
    } 
}