Java 捕獲進程被關閉

package DestroyProcessTest;

/**
 * @Author: ZhangHao
 * @Description: 銷燬進程測試
 * @Date: 2020/6/6 11:12
 * @Version: 1.0
 */
public class DestroyProcessTest {
    public static void main(String[] args) throws InterruptedException {
        new DestroyProcessTest().outOfMemoryTest();
    }

    private void activeExitTest() throws InterruptedException {
        // 程序正常結束時會觸發ShutdownHook
        Thread runtimeShutDownThread = new Thread(() -> System.out.println("DestroyProcessTest"));
        Runtime.getRuntime().addShutdownHook(runtimeShutDownThread);
        Thread.sleep(3000);
    }

    private void outOfMemoryTest() throws InterruptedException {
        // -Xmx=20m
        // 堆內存超過設置的上限時,會觸發ShutdownHook
        // -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./out
        // 內存溢出時生成的java_pid進程號.hprof默認在工程根目錄,-XX:HeapDumpPath可以更改目錄,hprof文件用jdk/bin/jvisualvm查看
        Thread runtimeShutDownThread = new Thread(() -> System.out.println("DestroyProcessTest"));
        Runtime.getRuntime().addShutdownHook(runtimeShutDownThread);
        Object[] objectArray = new Object[Integer.MAX_VALUE];
        for(int i = 0;i < objectArray.length;i++){
            objectArray[i] = new Object();
        }

        Thread.sleep(Long.MAX_VALUE);
    }

    private void exitTest() throws InterruptedException {
        // 手動關閉進程時不會觸發ShutdownHook
        Thread runtimeShutDownThread = new Thread(() -> System.out.println("DestroyProcessTest"));
        Runtime.getRuntime().addShutdownHook(runtimeShutDownThread);
        Thread.sleep(Long.MAX_VALUE);
    }
}
package DestroyProcessTest;

/**
 * @Author: ZhangHao
 * @Description: 銷燬進程測試
 * @Date: 2020/6/6 11:28
 * @Version: 1.0
 */
public class DestroyProcessTestThread extends Thread {

    public DestroyProcessTestThread() {
        this.setName("DestroyProcessTestThread");
    }

    public void start_server() {
        Runtime.getRuntime().addShutdownHook(this);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        System.out.println("DestroyProcessTest");
    }

    public static void main(String[] args) {
        DestroyProcessTestThread server = new DestroyProcessTestThread();
        server.start_server();
    }

}
package DestroyProcessTest;/**
 * @Author: ZhangHao
 * @Description: 銷燬進程測試
 * @Date: 2020/6/9 11:05
 * @Version: 1.0
 */

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class DestroyProcessTestApplication extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        // 關閉窗口會執行runtimeShutDownThread的run方法
        Thread runtimeShutDownThread = new Thread(() -> System.out.println("DestroyProcessTest"));
        Runtime.getRuntime().addShutdownHook(runtimeShutDownThread);
        // 關閉窗口監聽CloseRequest事件
        primaryStage.setOnCloseRequest(event -> System.out.println("DestroyProcessTest1"));

        Pane pane = new Pane();
        primaryStage.setScene(new Scene(pane, 300, 300));
        primaryStage.show();
    }
}

java 遇到未知異常使程序崩潰,輸出jvm的dump崩潰信息到指定文件,然後殺死此進程:https://blog.csdn.net/m0_37518406/article/details/78900393

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章