有时候咱们用到的程序不必定老是在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