推薦閱讀:
基本方法學習
線程池是我們經常使用的工具,也是面試必問的知識點,那麼如何優雅的關閉線程池那?
線程池相信大家都使用過,但是使用完成之後如何關閉線程池大家真的未必真的會使用。有人可能會說調用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。
優雅關閉線程池的正確姿勢
- step1:執行shutdown方法,等待所有任務執行完畢並拒絕新任務的提交。
- step2:執行awaitTermination(long timeout,TimeUnit unit),指定超時時間,判斷是是否已經關閉所有任務,防止線程永遠無法關閉。
- step3:如果step2返回fasle,或者被中斷。調用shutDownNow方法立即關閉線程池所有任務。
dubbo關閉線程池工具類學習
<code>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(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); } } }); } } }/<code>
作者:克里斯朵夫李維
鏈接:
https://juejin.im/post/5e9ec57df265da47c9171456
關鍵字: 優雅 return ExecutorService