I am writing a program where i am creating multiple threads in a process.
I need to handle that if the process is killed externally by someone by using kill -9 signal or Ctrl + C, my program should do some action before closing e.g. it should change the status of process to aborted in database.
How can i handle that ?
Do i need addShutdownHook()
? or is there any other better solution to my problem ?
I have added :
Runtime.getRuntime().addShutdownHook( new Thread() {
@Override
public void run() {
logger.info( "Running Shutdown Hook" );
//call some method
System.out.println( "Running Shutdown Hook" );
}
} );
inside my main method, But it doesn't seem to work.