线程池是我们经常使用的工具,也是面试必问的知识点,那么如何优雅的关闭线程池那?
线程池相信大家都使用过,但是使用完成之后如何关闭线程池大家真的未必真的会使用。有人可能会说调用 shutdown
或者 shutdownNow
就可以了,真的有那么简单吗?如果你关闭线程池的姿势不正确,最严重的情况会导致线程一直存在系统中。
shutDown:通知线程池启动有序关闭,执行线程池之前已经提交的任务,但是不再接受新的任务。调用shutDown后再提交任务将会抛出 RejectedExecutionException
异常。
shutDownNow:尝试立即停止所有已经提交的任务,并会返回正在等待执行(未执行)的任务列表。shutDownNow通过向线程池中的线程发送一个中断请求而中止线程,如果线程池中运行了会抛出 InterruptedException
的程序,将会抛出一个 InterruptedException
。如过这个线程不能响应中断那么可能永远无法被终止。
isTerminated:所有的任务都被关闭返回true,否则返回false。只有调用了shutDown或者shutDownNow, isTerminated
才可能为true。
awaitTermination(long timeout, TimeUnit unit) throws InterruptedException:阻塞当前线程直到
如果所有任务都关闭,则返回true,否则返回false。
public class ExecutorUtil { private static final Logger logger = LoggerFactory.getLogger(ExecutorUtil.class); private static final ThreadPoolExecutor shutdownExecutor = new ThreadPoolExecutor(0, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(100), new NamedThreadFactory("Close-ExecutorService-Timer", true)); public static boolean isTerminated(Executor executor) { if (executor instanceof ExecutorService) { if (((ExecutorService) executor).isTerminated()) { return true; } } return false; } /** * Use the shutdown pattern from: * https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html * @param executor the Executor to shutdown * @param timeout the timeout in milliseconds before termination */ public static void gracefulShutdown(Executor executor, int timeout) { if (!(executor instanceof ExecutorService) || isTerminated(executor)) { return; } final ExecutorService es = (ExecutorService) executor; try { // Disable new tasks from being submitted es.shutdown(); } catch (SecurityException ex2) { return; } catch (NullPointerException ex2) { return; } try { // Wait a while for existing tasks to terminate if (!es.awaitTermination(timeout, TimeUnit.MILLISECONDS)) { es.shutdownNow(); } } catch (InterruptedException ex) { es.shutdownNow(); Thread.currentThread().interrupt(); } if (!isTerminated(es)) { newThreadToCloseExecutor(es); } } public static void shutdownNow(Executor executor, final int timeout) { if (!(executor instanceof ExecutorService) || isTerminated(executor)) { return; } final ExecutorService es = (ExecutorService) executor; try { es.shutdownNow(); } catch (SecurityException ex2) { return; } catch (NullPointerException ex2) { return; } try { es.awaitTermination(timeout, TimeUnit.MILLISECONDS); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } if (!isTerminated(es)) { newThreadToCloseExecutor(es); } } private static void newThreadToCloseExecutor(final ExecutorService es) { if (!isTerminated(es)) { shutdownExecutor.execute(new Runnable() { @Override public void run() { try { for (int i = 0; i < 1000; i++) { es.shutdownNow(); if (es.awaitTermination(10, TimeUnit.MILLISECONDS)) { break; } } } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } } }); } } } 复制代码