shutdown ExecutorService And Await Termination - Java java.util.concurrent

Java examples for java.util.concurrent:ExecutorService

Description

shutdown ExecutorService And Await Termination

Demo Code


//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;
        }/*from  ww  w. j a  v  a 2  s.  c  om*/
        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();

    }
}

Related Tutorials