1. 程式人生 > >RunTime.getRunTime().addShutdownHook的用法

RunTime.getRunTime().addShutdownHook的用法

轉載自 https://www.liangzl.com/get-article-detail-8064.html

有時候我們用到的程式不一定總是在JVM裡面駐守,可能呼叫完就不用了,釋放資源.

RunTime.getRunTime().addShutdownHook的作用就是在JVM銷燬前執行的一個執行緒.當然這個執行緒依然要自己寫.

利用這個性質,如果我們之前定義了一系列的執行緒池供程式本身使用,那麼就可以在這個最後執行的執行緒中把這些執行緒池優雅的關閉掉.

比如我們定義了一個執行緒池

private ExecutorService streamThreadPool = Executors.newFixedThreadPool(streamNum);

然後我們需要對它進行優雅關閉

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銷燬前無論有沒有執行的執行緒都會進行中斷,然後關閉執行緒池.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

今天在閱讀Tomcat原始碼的時候,catalina這個類中使用了下邊的程式碼,不是很瞭解,所以google了一下,然後測試下方法,Tomcat中的相關程式碼如下:

      Runtime.getRuntime().addShutdownHook(shutdownHook);

   這個方法的含義說明:

       這個方法的意思就是在jvm中增加一個關閉的鉤子,當jvm關閉的時候,會執行系統中已經設定的所有通過方法addShutdownHook新增的鉤子,當系統執行完這些鉤子後,jvm才會關閉。所以這些鉤子可以在jvm關閉的時候進行記憶體清理、物件銷燬等操作。

 

一、編寫個測試類

  package com.test.hook;

public class TestShutdownHook {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // 定義執行緒1
  Thread thread1 = new Thread() {
   public void run() {
    System.out.println("thread1...");
   }
  };

  // 定義執行緒2
  Thread thread2 = new Thread() {
   public void run() {
    System.out.println("thread2...");
   }
  };

  // 定義關閉執行緒
  Thread shutdownThread = new Thread() {
   public void run() {
    System.out.println("shutdownThread...");
   }
  };

  // jvm關閉的時候先執行該執行緒鉤子
  Runtime.getRuntime().addShutdownHook(shutdownThread);

  thread1.start();
  thread2.start();
 }
}


列印結果:

thread2...
thread1...
shutdownThread...


或者:

thread2...
thread1...
shutdownThread...


結論:

 

無論是先列印thread1還是thread2,shutdownThread 執行緒都是最後執行的(因為這個執行緒是在jvm執行關閉前才會執行)。
--------------------- 
作者:O溺水的魚0 
來源:CSDN 
原文:https://blog.csdn.net/wgw335363240/article/details/5854402 
版權宣告:本文為博主原創文章,轉載請附上博文連結!