简体   繁体   English

如何在控制台程序终止自身之前调用方法

[英]How to invoke method before console program terminates itself

I would like to serialize some objects in console application just before program terminates itself(without adding this code simply in the end of main method)? 我想在程序终止自身之前在控制台应用程序中序列化一些对象(不只是在main方法的末尾添加此代码)吗?

Is that possible to solve out? 有可能解决吗?

You can register a shutDownHook as below: 您可以如下注册一个shutdownDownHook:

public class Task implements Runnable {
    public void run() {
        yourWork();
    }
}

public class App{
    static {
        Runtime.getRuntime().addShutDownHook(new Thread(new Task()));
    }
    public static void main(String[] args){
        .
        .
        .
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM