Code example for ExecutorService

Methods: awaitTerminationshutdownshutdownNow

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)
        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 
            // Preserve interrupt status 
Experience pair programming with AI  Get Codota for Java