Visitor :

May 4, 2009

addShutdownHook() of Runtime class



The addShutdownHook method of Runtime class is vervy useful to invoke something before a application exit. In addition coding in JUnit, very useful.
If you create and use some Threads, you can not invoke directly, this method usage is very helpful.


public class Monitor {
private static Log log = LogFactory.getLog(HTMLPageFetcher.class);

private static NotificationCollector notiCollector;
static {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
close();
}
});
}
public static void close() {
log.info("close application.!!");
notiCollector.stop();
}


...

No comments:

Post a Comment