shut down ExecutorService Quietly - Java java.util.concurrent

Java examples for java.util.concurrent:ExecutorService

Description

shut down ExecutorService Quietly

Demo Code


//package com.java2s;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

public class Main {
    public static final int DEFAULT_AWAIT = 5;

    public static void shutdownQuietly(ExecutorService executorService)
            throws InterruptedException {
        shutdownQuietly(executorService, DEFAULT_AWAIT);
    }//w w w .  j  ava 2 s  .c  o  m

    public static void shutdownQuietly(ExecutorService executorService,
            int await) {
        if (executorService != null && !executorService.isShutdown()) {
            executorService.shutdown();
            try {
                // try to allow any test tasks to finish
                if (!executorService.awaitTermination(await,
                        TimeUnit.SECONDS)) {
                    // some tasks still running. Try again more forcefully
                    executorService.shutdownNow();
                }
            } catch (InterruptedException e) {
                // ignore interrupt but attempt (again) to shutdown if current thread interrupted
                executorService.shutdownNow();
            }
        }
    }
}

Related Tutorials