平滑关闭线程池

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class ThreadPoolSmoothCloseUtil {
    public static void smoothClose(ExecutorService executorService, String executorServiceName, int timeOut, TimeUnit timeUnit) {
        log.info("executorService named={} begin smooth close",executorServiceName);
        try {
            executorService.shutdown();
            long beginShutdown = System.currentTimeMillis();
            while (!executorService.awaitTermination(timeOut, timeUnit)) {
                //继续等待直到确定关闭
                log.info("executorService.awaitTermination named= {} ",executorServiceName);
            }
            long endShutdown = System.currentTimeMillis();
            log.info("close executorService named={} totalTime ={}", executorServiceName, (endShutdown - beginShutdown));
        } catch (Exception e) {
            log.error("close executorService error named={} e=", executorServiceName, e);
        }
    }
}

你可能感兴趣的:(java,前端,开发语言)