JavaランタイムShutDownHookの説明


JavaランタイムShutDownHookの説明

 

アプリケーション:事前定義された操作は、プログラムが正常に実行されたとき、異常終了したとき、または強制終了したときに呼び出されます(system.exit())

 

**************************

関連クラス

 

ランタイム

public class Runtime {

****************
构造函数

    private Runtime() {}

****************
相关方法

    public static Runtime getRuntime() {
        return currentRuntime;
    }

    public void addShutdownHook(Thread hook) {
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            sm.checkPermission(new RuntimePermission("shutdownHooks"));
        }
        ApplicationShutdownHooks.add(hook);
    }

 

**************************

例:System.exit(int status)

 

public class Test {

    public static void main(String[] args){
        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            System.out.println("shutdown hook:"+System.currentTimeMillis());
        }));

        System.out.println(Thread.currentThread().getName()+"开始运行:"+System.currentTimeMillis());
        System.exit(0);
        System.out.println(Thread.currentThread().getName()+"运行结束:"+System.currentTimeMillis());

        try{
            Thread.sleep(1000);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

*******************

関連する出力

 

main开始运行:1584606724420
shutdown hook:1584606724438

 

 

**************************

例:プログラムは正常に終了します

 

public class Test {

    public static void main(String[] args){
        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            System.out.println("shutdown hook:"+System.currentTimeMillis());
        }));

        System.out.println(Thread.currentThread().getName()+"开始运行:"+System.currentTimeMillis());
        System.out.println(10/20);
        System.out.println(Thread.currentThread().getName()+"运行结束:"+System.currentTimeMillis());

        try{
            Thread.sleep(1000);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

*******************

関連する出力

 

main开始运行:1584606937250
0
main运行结束:1584606937266
shutdown hook:1584606938266

 

 

**************************

例:プログラムが異常終了する

 

public class Test {

    public static void main(String[] args){
        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            System.out.println("shutdown hook:"+System.currentTimeMillis());
        }));

        System.out.println(Thread.currentThread().getName()+"开始运行:"+System.currentTimeMillis());
        System.out.println(10/0);
        System.out.println(Thread.currentThread().getName()+"运行结束:"+System.currentTimeMillis());

        try{
            Thread.sleep(1000);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

*******************

関連する出力

 

main开始运行:1584607007657
shutdown hook:1584607007676
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at lifecycle.Test.main(Test.java:11)

 

 

387の元の記事を公開 98のような 30,000以上の訪問

おすすめ

転載: blog.csdn.net/weixin_43931625/article/details/104963174