Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想在程序自行终止之前序列化控制台应用程序中的一些对象(而不只是在 main 方法的末尾添加此代码)?
这有可能解决吗?
您可以注册一个shutDownHook,如下所示:
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){ . . . } }