有時候咱們用到的程序不必定老是在JVM裏面駐守,可能調用完就不用了,釋放資源.線程
RunTime.getRunTime().addShutdownHook的做用就是在JVM銷燬前執行的一個線程.固然這個線程依然要本身寫.server
利用這個性質,若是咱們以前定義了一系列的線程池供程序自己使用,那麼就能夠在這個最後執行的線程中把這些線程池優雅的關閉掉.ci
好比咱們定義了一個線程池資源
private ExecutorService streamThreadPool = Executors.newFixedThreadPool(streamNum);
而後咱們須要對它進行優雅關閉get
Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { shutdownGracefully(); } });
public void shutdownGracefully() { shutdownThreadPool(streamThreadPool, "main-pool"); } /** * 優雅關閉線程池 * @param threadPool * @param alias */ private void shutdownThreadPool(ExecutorService threadPool, String alias) { log.info("Start to shutdown the thead pool: {}", alias); threadPool.shutdown(); // 使新任務沒法提交. try { // 等待未完成任務結束 if (!threadPool.awaitTermination(60, TimeUnit.SECONDS)) { threadPool.shutdownNow(); // 取消當前執行的任務 log.warn("Interrupt the worker, which may cause some task inconsistent. Please check the biz logs."); // 等待任務取消的響應 if (!threadPool.awaitTermination(60, TimeUnit.SECONDS)) log.error("Thread pool can't be shutdown even with interrupting worker threads, which may cause some task inconsistent. Please check the biz logs."); } } catch (InterruptedException ie) { // 從新取消當前線程進行中斷 threadPool.shutdownNow(); log.error("The current server thread is interrupted when it is trying to stop the worker threads. This may leave an inconcistent state. Please check the biz logs."); // 保留中斷狀態 Thread.currentThread().interrupt(); } log.info("Finally shutdown the thead pool: {}", alias); }
這樣咱們就能夠在JVM銷燬前不管有沒有執行的線程都會進行中斷,而後關閉線程池.it