0

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)?

Is that possible to solve out?

Yoda
  • 17,363
  • 67
  • 204
  • 344
  • 2
    You can se `addShutdownHook(Thread hook)` to do it. – Aniket Thakur Mar 18 '14 at 09:27
  • 1
    Because it is then not guaranteed that the code will be executed when the program exits abnormally (e.g. by pressing Ctrl+C in the console). The code in shutdown hook will execute though. – hoefling Mar 18 '14 at 09:35

1 Answers1

0

You can register a shutDownHook as below:

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){
        .
        .
        .
    }
}
Vivek Kothari
  • 462
  • 6
  • 20