Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤和異常

Java 多執行緒

Java 同步

Java 網路

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 雜項

Java API 和框架

Java 類參考

Java 有用資源

Java - JVM 停止鉤子



JVM 關閉

以下是兩種不同的 JVM 關閉方式:

  • 受控過程:如果呼叫了System.exit() 方法,按下 CTRL+C,或最後一個非守護執行緒終止,則 JVM 開始關閉其程序。

  • 突然方式:如果 JVM 收到終止訊號,呼叫了Runtime.getRuntime().halt() 方法,或發生任何型別的作業系統恐慌,則 JVM 開始關閉其程序。

JVM 停止鉤子

停止鉤子只是一個已初始化但未啟動的執行緒。當虛擬機器開始其關閉序列時,它將以某種未指定的順序啟動所有已註冊的停止鉤子,並讓它們併發執行。當所有鉤子都完成後,它將執行所有未呼叫的終結器(如果已啟用退出時的終結)。最後,虛擬機器將停止。請注意,守護執行緒將在關閉序列期間繼續執行,如果透過呼叫 exit 方法啟動關閉,則非守護執行緒也將繼續執行。

JVM 停止鉤子:addShutdownHook(Thread hook) 方法

Runtime addShutdownHook(Thread hook) 方法註冊一個新的虛擬機器停止鉤子。

宣告

以下是java.lang.Runtime.addShutdownHook()方法的宣告

public void addShutdownHook(Thread hook)

引數

hook - 一個已初始化但未啟動的 Thread 物件。

返回值

此方法不返回值。

異常

  • IllegalArgumentException - 如果指定的鉤子已註冊,或者可以確定鉤子已在執行或已執行。

  • IllegalStateException - 如果虛擬機器已在關閉過程中。

  • SecurityException - 如果存在安全管理器並且它拒絕 RuntimePermission("shutdownHooks")。

JVM 停止鉤子的示例

在此示例中,我們透過擴充套件 Thread 類建立一個名為 CustomThread 的類。此執行緒物件將用作 JVM 停止鉤子。CustomThread 類具有 run() 方法的實現。在主類 TestThread 中,我們使用 Runtime.getRuntime().addShutdownHook() 方法添加了一個停止鉤子,方法是將執行緒物件傳遞給它。在輸出中,您可以驗證當程式即將退出時是否呼叫了 CustomThread run() 方法。

package com.tutorialspoint;

class CustomThread extends Thread {
   public void run() {
      System.out.println("JVM is shutting down.");
   }
}

public class TestThread {
   public static void main(String args[]) throws InterruptedException {
      try {
         // register CustomThread as shutdown hook
         Runtime.getRuntime().addShutdownHook(new CustomThread());
         // print the state of the program
         System.out.println("Program is starting...");
         // cause thread to sleep for 3 seconds
         System.out.println("Waiting for 3 seconds...");
         Thread.sleep(3000);
         // print that the program is closing
         System.out.println("Program is closing...");
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

輸出

Program is starting...
Waiting for 3 seconds...
Program is closing...
JVM is shutting down.

更多 JVM 停止鉤子的示例

示例 1

在此示例中,我們透過實現 Runnable 介面建立一個名為 CustomThread 的類。此執行緒物件將用作 JVM 停止鉤子。CustomThread 類具有 run() 方法的實現。在主類 TestThread 中,我們使用 Runtime.getRuntime().addShutdownHook() 方法添加了一個停止鉤子,方法是將執行緒物件傳遞給它。在輸出中,您可以驗證當程式即將退出時是否呼叫了 CustomThread run() 方法。

package com.tutorialspoint;

class CustomThread implements Runnable {
   public void run() {
      System.out.println("JVM is shutting down.");
   }
}

public class TestThread {
   public static void main(String args[]) throws InterruptedException {
      try {
         // register CustomThread as shutdown hook
         Runtime.getRuntime().addShutdownHook(new Thread(new CustomThread()));
         // print the state of the program
         System.out.println("Program is starting...");
         // cause thread to sleep for 3 seconds
         System.out.println("Waiting for 3 seconds...");
         Thread.sleep(3000);
         // print that the program is closing
         System.out.println("Program is closing...");
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

輸出

Program is starting...
Waiting for 3 seconds...
Program is closing...
JVM is shutting down.

示例 2

我們也可以使用 removeShutdownHook() 刪除停止鉤子。在此示例中,我們透過實現 Runnable 介面建立一個名為 CustomThread 的類。此執行緒物件將用作 JVM 停止鉤子。CustomThread 類具有 run() 方法的實現。在主類 TestThread 中,我們使用 Runtime.getRuntime().addShutdownHook() 方法添加了一個停止鉤子,方法是將執行緒物件傳遞給它。作為最後一條語句,我們使用 Runtime.getRuntime().removeShutdownHook() 方法刪除了鉤子。

package com.tutorialspoint;

class CustomThread implements Runnable {
   public void run() {
      System.out.println("JVM is shutting down.");
   }
}

public class TestThread {
   public static void main(String args[]) throws InterruptedException {
      try {
         Thread hook = new Thread(new CustomThread());
         // register Message as shutdown hook
         Runtime.getRuntime().addShutdownHook(hook);
         // print the state of the program
         System.out.println("Program is starting...");
         // cause thread to sleep for 3 seconds
         System.out.println("Waiting for 3 seconds...");
         Thread.sleep(3000);
         // print that the program is closing
         System.out.println("Program is closing...");
		  Runtime.getRuntime().removeShutdownHook(hook);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

輸出

Program is starting...
Waiting for 3 seconds...
Program is closing...
廣告