Sometimes you would like to run a code at close point of your application. The implementing is very easy and is just by adding a ShutdownHook to your application.
hier is the example:
public class RunAtEnd {
public static void main(final String[] args) {
System.out.println("Starting the program");
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
System.out.println("You've closed your program");
}
}));
System.out.print("Waiting 5 seconds ");
for (int i = 0; i < 5; i ++) {
System.out.print(".");
try {
Thread.sleep(1000);
}
catch (final InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("");
}
}
|
No comments:
Post a Comment